4 ms·
I just started learning haskell, can someone explain what: (\a0 -> (collatzLen a0, a0)) is doing?
by Nican 13y ago
I just started learning haskell, can someone explain what:
(\a0 -> (collatzLen a0, a0))
is doing?
- wisty 13y ago> 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.
- evincarofautumn 13y agoFunction application has high precedence in Haskell, so it would be (collatzLen(a0), a0).
- chas 13y agoThat 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.
- tsahyt 13y agochas 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 http://www.haskell.org/haskellwiki/Anonymous_function