I'm just getting started with Haskell. I'm trying to create a function that imitates the standard replicate
function in Haskell, but using recursion. For example,
Prelude> replicate 3 "Ha!"
["Ha!","Ha!","Ha!"]
It should be of type Int -> a -> [a]
. So far I have:
myReplicate :: Int -> a -> [a]
myReplicate x y = y : myReplicate (x-1) y
myReplicate 0 y = [ ]
However, my function always generates infinite lists:
Prelude> myReplicate 3 "Ha!"
["Ha!","Ha!","Ha!","Ha!","Ha!","Ha!","Ha!",...
You have to put the second case before the first, else it will never get to the second case.