[Haskell-cafe] Sequence differences

Ketil Malde ketil at malde.org
Fri Apr 10 15:52:09 EDT 2009


michael rice <nowgate at yahoo.com> writes:

> map :: (a -> b) -> [a] -> [b]    <==  I'm assuming this is correct

This is the type of 'map', yes.  Btw, ou can check types in GHCi with the
:i command.

> s f ls 
>
> seems much like
>
> map f ls
>
> but instead looks like   
>
> s :: (a -> a -> a) -> [a] -> [a]

If you look at the definition:

>> s f [] = []
>> s f [x] = [x]
>> s f l = [ a f b | (a,b) <- zip (init l) (tail l)]

You'll notice that the second clause, namely

   s f [x] = [x]

produces the second parameter [x] (of type [a]) as its output, and
thus the types must be the same as well. 

Also (assuming it is 'f a b' and not 'a f b' in the list
comprehension), f is applied to two parameters, so it'll have to be
of type (x -> y -> z), and since the two input parameters come from
the originating list, x and y must be the same as a, and since we have
seen the result list also has the same type, z must be the same as a,
too.  Thus f must have type (a -> a -> a).  Unclear? Clear? Operating
thetan? 

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants


More information about the Haskell-Cafe mailing list