On 10 Mar, Steve Frampton wrote:
>  My function looks sort of like this:
>  
>    foo :: Int -> [a]
>    foo 0 = []
>    foo x = ['1'] ++ foo(x - 1)

Since Haskell can infer types most of the time, try

>    foo 0 = []
>    foo x = ['1'] ++ foo(x - 1)

with this loaded into hugs you can then try

Main> :type foo
foo :: Num a => a -> [Char]
Main> 

which tells you the correct type.  The type you declared is too
general, because it means that foo would have to have the property that
whatever type of list was wanted in a given context, (foo n) would have
to return a list of that type.  But your foo returns a list of
characters, so clearly doesn't satisfy this.

The "Num a =>" indicates that the argument has to be a number; this is
inferred because you subtract one from it.

you could write

foo 0 = []
foo x = ['1'] ++ foo ((x::Int) - 1)

which gives

Main> :type foo
foo :: Int -> [Char]
Main> 

or write

foo :: Int -> [Char]
foo 0 = []
foo x = ['1'] ++ foo (x - 1)

to declare it yourself.

Note that type casting in the C sense is not available in Haskell,
the only thing you can do is to restrict something to have fewer types
than it otherwise would have.

  Jon

-- 
Jon Fairbairn                                 [EMAIL PROTECTED]
18 Kimberley Road                                        [EMAIL PROTECTED]
Cambridge CB4 1HH                      +44 1223 570179 (pm only, please)




Reply via email to