Re:An example of fast numerical computation using Haskell
by Nican on Saturday November 30, 2013 @12:43PM
(#6823805)
I just started learning haskell, can someone explain what:
(\a0 -> (collatzLen a0, a0))
is doing?
[ Parent
| on HN ]
Re:An example of fast numerical computation using Haskell
by chas on Saturday November 30, 2013 @01:01PM
(#6823865)
That is a function that takes a starting parameter and returns a pair with the length of the sequence as the first element and the starting value as the second element. This is mapped over a list of integers from 1 to max_a0 to find the length of the sequence starting with each integer.
Maximum finds the largest component of a list and compares on the first element of a pair first, so the pair with the longest sequence length as its first element and the starting value that produced that sequence length as it's second element will be returned by `maximum $ map (\a0 -> (collatzLen a0, a0)) [1..max_a0]`.
This function could also be expressed as `collatzLen >>= (,)`, if you were into that sort of thing.
Re:An example of fast numerical computation using Haskell
by wisty on Saturday November 30, 2013 @12:53PM
(#6823839)
> print $ maximum $ map (\a0 -> (collatzLen a0, a0)) [1..max_a0]
I think the last bit of the line is:
for a0 in range(1,max_a0):
collatzLen(a0,a0)
a0 is basically the loop variable.
Re:An example of fast numerical computation using Haskell
by evincarofautumn on Saturday November 30, 2013 @05:33PM
(#6824972)
Function application has high precedence in Haskell, so it would be (collatzLen(a0), a0).
Re:An example of fast numerical computation using Haskell
by tsahyt on Saturday November 30, 2013 @02:39PM
(#6824293)
chas explained it very well already, I'd just like to add that this is called a "lambda function" (or lambda expression) or an anonymous function, just in case you want to look it up. There's more on it here: http://www.haskell.org/haskellwiki/Anonymous_function