News for hackers. Stuff that matters.
Re:The New Haskell Homepage by berdario on Wednesday July 09, 2014 @01:15AM (#8008181)

    Num a => a
is actually (Num a) => (a)

that is, Num a means roughly something like

    template<class a>
    class INum{
    }
`a` is an instance of `Num`

if you want to write a times2 function(f(x) = x*2), its type would be (with redundant parenthesis added)

     (Num a) => (a -> a)
so, a->a means it's a function that takes an `a` and returns an `a`... and `a` can be any thing that implements Num

The idea with haskell is that you heavily rely on polymorphism on the return types, this might make using code a little more awkward, since you have to explicitly say the type that you want to constrain to, but it makes writing generic libraries/APIs a lot easier, you might find this question interesting:

http://programmers.stackexchange.com/questions/105662/is-ret...

     `[2..]`
is the same thing as

     itertools.count(2)
in Python, an infinite sequence of number, starting from 2

    sieve something where sieve (p:xs) = yadayada
is usually written on multiple lines

    sieve something
      where
        sieve (p:xs) = yadayada
if you know that you can define a function with

    f x = something_with_x
it's somewhat obvious that you're defining a function called sieve that takes a (p:xs)

so, you're just defining a function and using it (with the [2..]) argument on the same line

   (p:xs)
is destructuring, it basically take a list (`(:)` is used to `cons`truct lists) and assign the first element of it to `p`, and the rest to `xs` (xs is a commonly used name for this in Haskell)

so, it's like calling

    sieve(list);
but you can define its signature as

    something sieve(T head, list<T> rest);
(`something` will turn out to be `list<T>`)

    /=
is indeed the same as !=, so (x `mod` p /= 0) is just (x is not a multiple of p)

    [x | x <- xs, x `mod` p /= 0]
is a list comprehension, and is taking all the elements from xs that aren't multiples of p

p, as suggested by the letter, is a prime... so you're recursively filtering the elements who are not multiples of p, leaving only the ones who are multiple only of 1 and p themselves (that is, primes)

finally, you're concatenating the prime you're currently acting on (p) with the infinite lazy list of all subsequent primes

Hope this helps

There are no comments on this comment yet.