Hacker Newsnew | past | comments | ask | show | jobs | submit | ostso's commentslogin

    unset -f command_not_found_handle


For more on monoids, see Brent Yorgey's paper _Monoids: Theme and Variations (Functional Pearl)_ at <http://www.cis.upenn.edu/~byorgey/pub/monoid-pearl.pdf>; (there's also a video of his talk at the Haskell Symposium at <http://www.youtube.com/watch?v=X-8NCkD2vOw>).


Perhaps mistercow means things like

  def foo():
    x = 5
    def bar():
      print x
    def baz():
      x = 6
      print x
    bar()
    baz()
    print x
Which prints 5, 6, 5. If baz tried to print x before assigning, it would be an UnboundLocalError.


Not really -- "any" and "filter" are total functions, but "first" of an empty list is undefined.

On the other hand, that also applies to foldl1 and {max,min}imum{,By}, so it's probably not the reason.

The reason is probably that in JavaScript you can just say a[0] to get the first element element of a.


It seemed unnecessary, yes, although I suppose there are possibly some circumstances where one might want a `first` function—for example if one were folding over a list of lists.


There are an infinite number of circumstances where one would want 'first'.

In Lisp terms[1], 'first' is 'car', which, in conjunction with 'cdr'. With that combination recursive logic is regularly implemented.

[1] Some Lisps already include this alias.


In most cases the "a[0]" approach works, however. The set of circumstances where you want "first" as a proper function is limited to those cases where you need to pass it into some higher order function (which is still infinite, granted, but it's not 'every time we'd want "car"').


You should add the word "web" to the title. There are many other kinds of caching.


That's a fixed-point combinator, but it's not actually the Y combinator; in fact, it defeats much of the purpose of Y, since it's defined using explicit recursion, which Y is designed to avoid.

If you just want a clear definition of a fixed-point combinator, you might as well use a non-strict language like Haskell, which doesn't require you to eta-expand g. Here's one recursive definition of fix in Haskell:

    fix f = f (fix f)
This is probably the clearest definition you could ask for of what a fixed-point combinator actually is (other than something like "fix f = f (f (f (f (f ...", which is more difficult to give to a computer).

Here's another one, which closer to what you gave in JavaScript (this is what you'll actually find in the Haskell standard libraries):

    fix f = let x = f x in x
(Of course, due to type-checking, it's non-trivial to define the actual non-recursive Y in Haskell; you usually have to resort to using some type-level recursion. But in an untyped non-strict language you could define it easily enough.)


"bar none" is a little excessive -- Haskell's syntax is probably even cleaner. Compare:

  1. map (*2) [1..10]

  2. sum [1..1000]
     -- sum = foldl (+) 0

  3. any (`isInfixOf` tweet) wordlist

  4. fileText <- readFile "data.txt"
     fileLines <- lines <$> readFile "data.txt"

  5. mapM_ putStrLn . map (\i -> "Happy Birthday " ++ if i == 3 then "dear NAME" else "to You") $ [1..4]

  6. (passed, failed) = partition (>60) [49,58,76,82,88,90]

  7. -- I avoid XML so I don't know what library you'd use here.

  8. minimum [14, 35, -7, 46, 98]
     -- minimum = foldl1 min


"bar none" is a little excessive -- Haskell's syntax is probably even cleaner. Compare

And ditto for Io (http://www.iolanguage.com):

  1.  1 to(10) map(*2)

  2.  1 to(1000) asList sum

  3.  tweet findSeqs( wordlist )

  4.  fileText  := File with("data.txt") open readLines join
      fileLines := File with("data.txt") open readLines


  5.  4 repeat (i, writeln("Happy Birthday " .. if(i == 2, "dear NAME", "to you")))

  6.  list(49, 58, 76, 82, 88, 90) partition(x, x > 60)


  7.  results := SGML URL with("http://search.twitter.com/search.atom?&q=scala") 
        fetch asXML

  8.  list(14, 35, -7, 46, 98) min
      list(14, 35, -7, 46, 98) max
Some notes:

i) For 1 & 2 remember to have `Range` loaded first. ii). OK I cheated on 6 because there is no partition currently in the Io core lib. Here is a "simple" way I did it for the example:

    List partition := method (
    
        returnThis := Object clone do (
            passed := list()
            failed := list()
        )
    
        argSlot := call message argAt(0)
        cond    := call message argAt(1)
    
        self foreach (n,
            newSlot(argSlot asString, n)
            if (doMessage(cond), 
                returnThis passed append(n), 
                returnThis failed append(n))
        )
    
        returnThis
    )


No, because robbing someone of their incentive to work is a net loss to society, so the bandits would no longer be perfect (unless they in turn gain a corresponding incentive to work, or some other equivalent gain).

There are no perfect bandits, let along a society full of them. Scenarios like "If all members of a society were perfect bandits" are hypothetical and don't really translate to any actual human society that you could imagine. Even so, they're useful for illustrating concepts.


This title is designed to get people to click it (to discover what "The One Group" is), rather than to be informative. Please add e.g. ": Developers" to the end of it, so that people can click on it only if they actually want to read the article.


The difference is that the whole point of the Y combinator is that it's a fixed-point combinator that doesn't use explicit recursion. Imagine that you didn't have "var", and couldn't name anything (which is the situation in, say, the lambda calculus). Using the Y combinator you could still define recursive functions.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: