[Haskell-beginners] A simple function V2

Ertugrul Söylemez es at ertes.de
Fri Aug 31 14:37:16 CEST 2012


Ezequiel Hernan Di Giorgi <hernan.digiorgi at gmail.com> wrote:

> *intercalate :: (Eq t) => [t] -> [t] -> [t]*
> *intercalate (x:xs) (y:ys)*
> * | xt == [] = []*
> * | yt == [] = []*
> * | otherwise = x : y : intercalate xs ys*
> * where xt=(x:xs)*
> *            yt=(y:ys)*

As a beginner it's almost always wrong to use the comparison operator
(==).  Use it only to compare numbers, for everything else use
pattern-matching.  In particular use guards ("| ... = ...") only when
they are really necessary.  In this case they are not.

Put differently, if your function needs an Eq constraint (or any
constraint) you're doing it wrong.  The type signature should be:

    intercalate :: [a] -> [a] -> [a]


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120831/66c9e2d9/attachment.pgp>


More information about the Beginners mailing list