I'm not going to go to the algorithm itself, but here are some tips on how to structure recursive functions.
First, here's how you format your code in a separate file
fibo :: Integral x => [x]->x->x->x->[x] fibo lxy 0 = l fibo lxyn = fibo (l ++ [y+x] ++ [y+x+y]) (x+y) (y+x+y) (n-1)
If you save this as fibo.hs then you can start GHCi with
ghci fibo.hs
to load the file at startup. You can also upload the file after starting GHCi with the command
:l fibo.hs
(if you run GHCi in the same directory where you saved the fibo.hs file)
Another nice feature is that now that you are editing a file, you can reload all your changes just by entering
:r
at the invitation of GHCi.
Now, to get rid of additional parameters, the usual template in Haskell is to reorganize the recursive part into its own helper function and have the main function as an entry point that accepts only the necessary parameters. For example,
fibo :: Integral x => x -> [x] fibo n = fiboHelper [0,1] 0 1 n fiboHelper :: Integral x => [x]->x->x->x->[x] fiboHelper lxy 0 = l fiboHelper lxyn = fiboHelper (l ++ [y+x] ++ [y+x+y]) (x+y) (y+x+y) (n-1)
Now you can call fibo just with
> fibo 3 [0,1,1,2,3,5,8,13]
In addition, a helper function like this, which is not useful in itself, is usually hidden inside the main function as an internal function using let or where .
fibo :: Integral x => x -> [x] fibo n = fiboHelper [0,1] 0 1 n where fiboHelper lxy 0 = l fiboHelper lxyn = fiboHelper (l ++ [y+x] ++ [y+x+y]) (x+y) (y+x+y) (n-1)
An internal function like this is usually assigned a shorter name because the context makes its purpose clear, so we could change the name, for example. fibo' .
go is another commonly used name for a recursive helper function.