Re:A philosophical difference between Haskell and Lisp (2015)
by garethjrowlands on Sunday August 02, 2020 @05:33PM
(#24031968)
For those that didn't know, Haskell does of course have list comprehensions:
Here's an example finding consonants:
[ Parent
| on HN ]
Re:A philosophical difference between Haskell and Lisp (2015)
by centimeter on Sunday August 02, 2020 @06:18PM
(#24032267)
No need for the dollar sign in the second example :)
A nice thing about Haskell list comprehensions is they're based on a trivial transformation to monad syntax, which makes it easier to factor out complex list transformations into multiple little bites. I've run into this problem in Python. Also, Python list comprehensions can often behave very unexpectedly due to mutability.
Here's an example of the monad syntax:
[(x,y) | x <- [1..10], y <- [1..x], odd (x + y)]
equiv do
x <- [1..10]
y <- [1..x]
guard $ odd (x + y)
return (x,y)
Lots of combinatorics problems can be elegantly solved using the list monad like that.
Re:A philosophical difference between Haskell and Lisp (2015)
by mypalmike on Sunday August 02, 2020 @08:11PM
(#24032894)
Example where Python list comprehensions behave very unexpectedly...?
Re:A philosophical difference between Haskell and Lisp (2015)
by dwohnitmok on Sunday August 02, 2020 @06:08PM
(#24032202)
Haskell's list comprehensions actually started off as more general monad comprehensions, were later restricted to lists, and are back behind a GHC language pragma.