45 ms·
Yes. You can say a = zipWith (+) (0 : b) (tail c ++ [0]) and that would make a[i] = b[i-1] (where b[-1] is 0) + c[i+1] (EDIT: where the last element of c is
by lsb 15y ago
Yes. You can say
a = zipWith (+) (0 : b) (tail c ++ [0])
and that would make a[i] = b[i-1] (where b[-1] is 0) + c[i+1] (EDIT: where the last element of c is 0 (thanks tkahn6!)).
Also, interestingly, if you have lazy evaluation, you can use a zip to produce the fibonacci numbers: just zip the list up with itself.
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
so you cons 0 to 1 to the zip, and the zip is combining 0 with 1, and then 1 with 1, and then 1 with 2.
fibs[0] = 0
fibs[1] = 1
fibs[i, i>=2] = fibs[i-2] + fibs[i-1]
and this looks like the math behind it.
- deleted 15y ago[deleted]