Hacker News
new
|
past
|
comments
|
ask
|
show
|
jobs
|
submit
login
Smaug123
on July 15, 2020
|
parent
|
context
|
favorite
| on:
Data structures and algorithms I actually used whi...
I, for one, prefer recursion to explicit iteration in most cases. Anyway, if you were going to write the answer in Haskell, it's a three-liner and it's recursive.
eru
on July 15, 2020
[–]
Why do you need so many lines?
fib = 1 : zipWith (+) (0 : fib) fib
exdsq
on July 15, 2020
|
parent
[–]
Because the three liner is easier to read
eru
on July 15, 2020
|
root
|
parent
[–]
Btw, which three-liner do you mean?
exdsq
on July 17, 2020
|
root
|
parent
[–]
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
eru
on July 18, 2020
|
root
|
parent
[–]
Oh, that version takes exponential time to compute. The one-liner I gave computes up to the n-th Fibonacci number in linear time.
exdsq
on July 18, 2020
|
root
|
parent
|
next
[–]
Your version is definitely more performant, but the three liner is just a bit nicer to read (especially for beginners) :)
eru
on July 18, 2020
|
root
|
parent
|
next
[–]
Yes. It depends on what you want to show. Your version shows how to define functions and some simple pattern matching.
exdsq
on July 21, 2020
|
root
|
parent
|
next
[–]
Having worked with Haskell for a year now I still can't read much more than functions and pattern matching! :D
Smaug123
on July 18, 2020
|
root
|
parent
|
prev
[–]
Not in Haskell, it doesn't. Haskell stores everything for you, right?
tome
on July 19, 2020
|
root
|
parent
[–]
No, it doesn't.
Guidelines
|
FAQ
|
Lists
|
API
|
Security
|
Legal
|
Apply to YC
|
Contact
Search: