Trish
Trish

Reputation: 11

Haskell conversion to a list

I am just staring with Haskell!!! Assume I have a function that recursively calls itself to generate a series

Assume a simple example of sum:

func n | n==0 = 0
       | n==1 = 1
       | otherwise = func(n-1) + func(n) 

So if I call sum 3, it outputs the value 6.

How would I be able to generate a list [1,2,3] if sum 3 is called instead of giving the final output. I need to carry out list operations

When I write the following, I get an error-- parse error on input |

toList n | n >0 = let funcList = map func [0..]

Upvotes: 0

Views: 87

Answers (1)

ErikR
ErikR

Reputation: 52067

Use map:

funcList = map func [0..]

So now funcList !! n will have the same value as func n, i.e. it is as if you wrote:

funcList = [ func 0, func 1, func 2, func 3, ...]

Upvotes: 1

Related Questions