[Haskell-beginners] Case vs Guards. I still don't know what is the difference

Semih Masat masat.semih at gmail.com
Thu Jul 7 00:43:42 UTC 2016


Hello,

I am new to Haskell and trying to learn it with learnyouahaskell.com and
Pluralsight Haskell course.

And i have a very noob question.

I understand that *if .. else* is just a syntactic sugar over *case. *But
what about guards then ?

Are guards also *case *in different syntax ? Or vice versa ? Like with an
example.


anyEven nums
    | (length (removeOdd nums)) > 0 = True
    | otherwise                     = False


anyEven' nums = case (removeOdd nums) of
    []        -> False
    (x:xs)  -> True

I can do the same thing with both of them.

As i understand the only different thing is, with *case *i can manipulate
the parameter (like here in the example i used removeOdd) and can use the
manipulated parameter to decide what to do after that.
So i will not need to use removeOdd function inside the case. ( maybe i
will need to use in every guard definition if i choose to use guards )

Is this it?

Is this the only difference between them ?

And if it is, why haskell needed do implement both of them. Can't we use
function like removeOdd before using it on case or guard functions ?


Thanks, and sorry if my english is bad.

Semih Masat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160707/2bacca53/attachment.html>


More information about the Beginners mailing list