[Haskell-beginners] Re: Defeating type inference

Will Ness will_n48 at yahoo.com
Fri Feb 27 17:59:05 EST 2009


Philip Scott <pscott <at> foo.me.uk> writes:

> 
> Hi Alex,
> > The problem is that when you go fro
> >> months = range (Jan,Dec) : months
> >> realmonths = concat months
> >>     

Another way to go about this is to always look at types. You've correctly 
surmised that you have to refer back to the name you're defining, months, in a 
recusive definition, so let's write it down as 
  months = range (Jan,Dec) `somehowJoinWith` months

What is somehowJoinWith's type? Its first argument is [Month], let's call it 
[a]. So
 somehowJoinWith :: [a] -> b -> b

But you want its result to be a list of Month's too. So b = [a] and
 somehowJoinWith :: [a] -> [a] -> [a]

Cons (:) simply won't fit here, as it is :: a -> [a] -> [a].

(++) OTOH does fit, and is exactly what you need.

Thinking with types helps clarify the problem always. It helps us not to 
confuse oranges and boxes-of-oranges. :)

Cheers,



More information about the Beginners mailing list