How does this Haskell function work? -
i struggling see how function worked. nth number should calculate sum of previous 3 elements.
f' :: integer->integer f' = helper 0 0 1 helper b c 0 = helper b c n = helper b c (a+b+c) (n-1)
thanks time
say called f' 5
below sequence in executed:
iteration 1: helper 0 0 1 5
iteration 2: helper 0 1 (0+0+1) 4
iteration 3: helper 1 1 (0+1+1) 3
iteration 4: helper 1 2 (1+1+2) 2
iteration 5: helper 2 4 (1+2+4) 1
iteration 6: helper 4 7 (2+4+7) 0 => 4
Comments
Post a Comment