Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell fillZeros program [closed]

Tags:

haskell

the program adds n number of zeros to the list x example

fillZeros "100" 1 "0100" fillZeros "10" 4 "000010"

my code :

fillZeros :: (Eq a, Num a) => [Char] -> a -> [Char] 
fillzeros x 0 = x 
fillzeros (x:xs) a  = (0:fillzeros (x:xs) a-1)

Missing binding for variable "fillZeros" in type signature this error appears every time I reload :r

like image 865
Peter Avatar asked Aug 10 '26 22:08

Peter


1 Answers

The first problem you encounter is that you specify the signature for a function fillZeros (with uppercase Z), but your implementation is for a function fillzeros (with lowercase z).

Furthermore it makes no sense to use (0: fillzeros (x:xs) a-1): this is parsed as (0: (fillzeros (x:xs) a) - 1), so you subtract one from the recursive call, and furthermore 0 is not a Char so you can not use that in your function.

In order to pattern match, it is better to match with x, otherwise fillzeros "" 15 will raise an error, because the empty string does not match with (x:xs).

You thus implement this with:

fillzeros :: (Eq a, Num a) => [Char] -> a -> [Char] 
fillzeros x 0 = x 
fillzeros x a  = ('0' : fillzeros x (a-1))
like image 77
Willem Van Onsem Avatar answered Aug 12 '26 21:08

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!