Live data from Hacker News

The New Haskell Homepage

new-www.haskell.org

61–70 of 258 posts

Re: The New Haskell Homepage

#61
post #57

Natural, declarative, statically typed code. Not sure how that first adjective applies.

Looks like you're being downvoted, but I have to agree with you, especially given some of the examples right at the top.

  foldr (:) [] [1,2,3] 
There is absolutely nothing natural about that line of code unless you happen to already have some background. I executed that in the sandbox and got:

  [1,2,3]:: Num a => [a]
And I still don't know what was achieved.

Re: The New Haskell Homepage

#62
post #59
post #51

Earlier quoted context omitted.

Also maybe pick a simpler example and not play into the stereotype that Haskell is for people who think they are smarter than everyone else.

Could you suggest a simpler example? Finding primes is something that is taught in the first programming class in Indian high schools. I guess I've never thought of it as something hard. I looked at nodejs.org, and their first example is a web server! Python has the Fibonacci as it's second example (the first one show's numeric operations). Ruby does simple string operations on it's home page. While I think that is i…

Well, for one, it's a bad sieve algorithm.

I think a neat algorithm to demonstrate laziness and Haskell clarity would be enumerating the Calkin-Wilf rationals. [0] It's quite a bit longer but demonstrates a number of neat ideas. I'll start first with a derivation which demonstrates all of the structure of the algorithm and then go through a series of mechanical transforms so that by the end I have a one-liner and a comparable Python implementation.

The first algorithm comes directly from the paper and uses an intermediary infinite tree to represent the rationals.

    data BTree a = Node a (BTree a) (BTree a)

    fold :: (a -> x -> x -> x) -> BTree a -> x
    fold f (Node a l r) = f a (fold f l) (fold f r)

    unfold :: (x -> (a, x, x)) -> x -> BTree a
    unfold f x = let (a, l, r) = f x in Node a (unfold f l) (unfold f r)

    breadthFirst :: BTree a -> [a]
    breadthFirst = concat . fold glue where
      glue a ls rs = [a] : zipWith (++) ls rs

    allRationals :: Fractional a => [a]
    allRationals = breadthFirst (unfold step (1, 1)) where
      step (m, n) = ( m/n, (m, m+n)
                         , (n+m, n) )
In 16 lines I've got an infinite binary tree, its natural fold and unfold, a breadth first search, and a lazy algorithm for generating all of the rationals with no repeats. The whole thing is simple, natural, beautiful, and efficient! It demonstrates infinite recursive types, laziness, higher-order functions, and bounded polymorphism.

And also a neat algorithm!

The downside is that 16 lines is pretty long.

By inlining the fold and unfold I can get it down to 9 lines:

    data BTree a = Node a (BTree a) (BTree a)

    breadthFirst :: BTree a -> [a]
    breadthFirst = concat . glue where
      glue (Node a ls rs) = [a] : zipWith (++) (glue ls) (glue rs)

    rats :: Fractional a => [a]
    rats = breadthFirst (generate (1, 1)) where
      generate (m, n) = Node (m/n) (generate (m, m+n)) (generate (n+m, n))
If I'm allowed imports we can use Data.Tree and make this a one-liner!

    import Data.Tree

    allRationals :: Fractional a => [a]
    allRationals = flatten (unfoldTree step (1, 1)) where
      step (m, n) = ( m/n, [ (m, m+n), (n+m, n) ] )
Finally, if I go another route and fuse the fold and unfold together into a hylomorphism

    data Trip a x = Trip a x x deriving Functor

    hylo :: Functor f => (f b -> b) -> (a -> f a) -> a -> b
    hylo phi psi = phi . fmap (hylo phi psi) . psi

    allRationals :: Fractional a => [a]
    allRationals = concat (hylo glue step (1, 1)) where
      glue (Trip a ls rs) = [a] : zipWith (++) ls rs
      step (m, n) = Trip (m/n) (m, m+n) (n+m, n)
we can hide the tree entirely and demonstrate `deriving`... at considerable cost to clarity! With a little more golfing (read: inlining) we arrive at this beauty:

    allRationals :: Fractional a => [a]
    allRationals = concat (go (1, 1)) where
      go               = glue . next . step
      next (a, b, c)   = (a, f b, f c)
      glue (a, ls, rs) = [a] : zipWith (++) ls rs
      step (m, n)      = ( m/n, (m, m+n), (n+m, n) )
which at least has the bonus of demonstrating some nice co-recursion between go and next. Or even, ultimately:

    allRationals :: Fractional a => [a]
    allRationals = concat (go 1 1) where go m n = [m/n] : zipWith (++) (go m (m+n)) (go (n+m) n)
which is actually kind of nice again if almost all of the structure has vanished.

Note that if `interleave` were part of the Prelude then we could write

    allRationals :: Fractional a => [a]
    allRationals = go 1 1 where go m n = (m/n) : interleave (go m (m+n)) (go (n+m) n)
given

    interleave :: [a] -> [a] -> [a]
    interleave []     ys     = ys
    interleave xs     []     = xs
    interleave (x:xs) (y:ys) = x : y : interleave xs ys
which is a little prettier and directly comparable to something Pythonic like

    from fractions import Fraction
    from itertools import islice

    def interleave(x, y):
      while True:
        yield x.next()
        yield y.next()

    def all_rationals():
      def go(m, n):
        yield (m/n)
        for v in interleave(go(m, m+n), go(m+n, n)):
          yield v
      return go(Fraction(1,1), Fraction(1,1))

    def rationals(n):
      return list(islice(all_rationals(), n))
[0] http://www.cs.ox.ac.uk/jeremy.gibbons/publications/rationals...

Re: The New Haskell Homepage

#64
post #46

Please note this homepage is NOT final and it's going to see revisions before we push it out to the actual website, including many tweaks to the content and probably some styling tweaks too. There are a lot of other things we still need to do as well, like ensure all redirects and subpages work properly. Source: I'm one of the Haskell.org administrators, and we pushed this out only today.

Is there a practical reason why there's so much empty space with this new design, and why so little valuable content and functionality is visible by default? Viewing the existing site in a desktop browser, I get to see the description of Haskell, and a bunch of useful links about learning it, downloading an implementation, using it, and participating in the community. Recent news items and upcoming events are also vi…

The existing site is a pretty unappealing design and is extremely overcrowded; this has been complained about for years.

Re: The New Haskell Homepage

#65
post #62
post #59

Earlier quoted context omitted.

Could you suggest a simpler example? Finding primes is something that is taught in the first programming class in Indian high schools. I guess I've never thought of it as something hard. I looked at nodejs.org, and their first example is a web server! Python has the Fibonacci as it's second example (the first one show's numeric operations). Ruby does simple string operations on it's home page. While I think that is i…

Well, for one, it's a bad sieve algorithm. I think a neat algorithm to demonstrate laziness and Haskell clarity would be enumerating the Calkin-Wilf rationals. [0] It's quite a bit longer but demonstrates a number of neat ideas. I'll start first with a derivation which demonstrates all of the structure of the algorithm and then go through a series of mechanical transforms so that by the end I have a one-liner and a c…

It's also utterly incomprehensible for someone who hasn't seen Haskell before. Whereas with the existing example, one can at least piece together an idea of what's going on.

The point is to demonstrate the directness of expression and conciseness of Haskell, not to show how to create an efficient implementation of an involved algorithm.

Re: The New Haskell Homepage

#67
post #25

Another suggestion would be more examples under the clear concise code bit--possibly in a carousel. Additionally, all examples could be loaded into the "Try It" section, so I could type `take 4 primes` or something. Instead, in my attempt to load the primes function.. I was met with this bit. λ let sieve (p:xs) = p : sieve [x | x

I agree, but I'd go even further: make the example in the corner actually tryable the "Try It" section.

For this example:

  primes = sieve [2..]
      where sieve (p:xs) = 
        p : sieve [x | x 
I tried to type it into the shell:

  λ primes = sieve [2..]
  :1:8: parse error on input `='
It doesn't work. Okay, what if I copy and paste?

  λ primes = sieve [2..] where sieve (p:xs) = p : sieve [x | x :1:8: parse error on input `='
Doesn't work.

Now, I know enough about Haskell to know what I can and can't type into ghci, but what about people who are encountering Haskell for the first time? They'll try to run the given example in the "Try It" section and will get nothing but errors. Just my two cents.

P.S. Is Haskell still avoiding success at all costs? (A philosophy I continue to be okay with, but it seems to getting futile :) )

Re: The New Haskell Homepage

#68
post #57

Natural, declarative, statically typed code. Not sure how that first adjective applies.

Looks like you're being downvoted, but I have to agree with you, especially given some of the examples right at the top. foldr (:) [] [1,2,3] There is absolutely nothing natural about that line of code unless you happen to already have some background. I executed that in the sandbox and got: [1,2,3]:: Num a => [a] And I still don't know what was achieved.

For a function f and a value x,

    foldr f x [a,b,c]
gets turned into

    f(a, f(b, f(c, x)))
'(:)' is list concatenation, so the result is

    1 : (2 : (3 : []))
(Here we are writing list concatenation in infix notation, rather than the customary prefix notation). '[1,2,3]' is shorthand for 1 : (2 : (3: [])) in Haskell.

Re: The New Haskell Homepage

#69
post #59
post #51

Earlier quoted context omitted.

Also maybe pick a simpler example and not play into the stereotype that Haskell is for people who think they are smarter than everyone else.

Could you suggest a simpler example? Finding primes is something that is taught in the first programming class in Indian high schools. I guess I've never thought of it as something hard. I looked at nodejs.org, and their first example is a web server! Python has the Fibonacci as it's second example (the first one show's numeric operations). Ruby does simple string operations on it's home page. While I think that is i…

Fibonacci sounds perfect!

  fibonacci :: Integer -> Integer
  fibonacci 0 = 0
  fibonacci 1 = 1
  fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)
Arguments for this:

* "Find the Nth Fibonacci Number" is among the most universally known programming tasks, so visitors are far more likely to immediately pick up the example than they are with sieve.

* It shows off a bit of Haskell syntax that (A) can be learned just by looking at an example like this, (B) has a clear benefit to readability that any programmer can appreciate, and (C) is a syntax not found in most mainstream languages.

* The visitor needs no functional programming experience to follow it; it doesn't even use any higher-order functions! This is important, as many visitors will be completely new to FP, and an example that they can't follow is not going to be effective at encouraging them to continue reading.

Re: The New Haskell Homepage

#70
Looks really good, but as others noted, the examples should really work `as is` within the REPL. Also I think wikipedia has better example code. Everybody knows fibonacci and can compare it.

Please add a nice Haskell facts and features tab, like: Appeared in 1990; 24 years ago More facts and features on: http://en.wikipedia.org/wiki/Haskell_(programming_language)

Post reply on HN