Reputation: 9061
I am trying to figure out how many digits the 500th Fibonacci number is:
fib n = fibs (0,1) !! n where
fibs (a,b) = a:fibs(b,(a+b))
length (show (fib 500))
This does work. Is there a way to get rid of the parenthesis in the third line? Say
length . show . fib 500
or something alike?
This dollar-form works too:
length $ show $ fib 500
Generally, what is the recommended way in this situation?
Upvotes: 2
Views: 302
Reputation: 15121
You almost got it:
length . show . fib $ 500
You can define a function in pointless style for this
lengthOfFib = length . show . fib
then use it like this
lengthOfFib 500
Upvotes: 5
Reputation: 8136
I would normally use dots to compose all of the functions except the last one. For example:
length . show $ fib 500
this . that . theOther . length . show $ fib 500
You could also write it as:
(length . show . fib) 500
(this . that . theOther . length . show . fib) 500
Upvotes: 3
Reputation: 55069
Typical style:
length . show $ fib 500
Only compositions:
length . show . fib $ 500
One advantage to preferring composition is that you can easily pull out common subfunctions. Maybe you need length . show
somewhere else, so you can trivially move it elsewhere and give it a name; show . fib
is probably less useful, though. :)
What you said with:
length . show . fib 500
Is the same as:
\x -> length (show (fib 500)) x
Which the compiler sees as nonsensical because you’re using Int
like a function.
Upvotes: 11