<div dir="ltr">Thank you Theodore and Tushar.<div><br></div><div>Great examples and help.</div><div><br></div><div><br></div><div>I am understanding better now. </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 7, 2016 at 5:12 AM, Tushar Tyagi <span dir="ltr"><<a href="mailto:tushar4r@gmail.com" target="_blank">tushar4r@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div>It's great that you understood that the idiom of using case expressions is with pattern matching -- using it to count numbers is like using a crane to pick a packet of sugar -- pretty inefficient.<br><br>The case expression/pattern matching is very powerful and expressive. And I will be using a single example for multiple definitions (which you advised against), hoping that you see the difference between different idioms.<br><br>Take a look at the definition of map below:<br><br>map' f xs = case xs of<br>  [] -> []<br>  (y:ys) -> f y : map' f ys<br><br></div>Without pattern matching you will be using library functions like `null`, `head`, or `tail` and one of the downsides of these is that they crash the program in case `head` and `tail` are used with empty list without null checking. With the null check, the program looks inelegant (and is not the standard idiom):<br><br>map'' f xs = if null xs<br>             then []<br>             else let hd = head xs<br>                      tl = tail xs in<br>                  (f hd) : (map'' f tl)<br><br></div>You can clearly see the superior one.<br><br></div>Of course, pattern matching is not used only with case expressions: here is the definition of drop with and without case:<br><br>drop'' n xs = case (n, xs) of<br>  (0, xs) -> xs<br>  (_, []) -> []<br>  (n, y:ys) -> drop'' (n-1) ys<br><br>drop' 0 xs = xs<br>drop' _ [] = []<br>drop' n (x:xs) = drop' (n-1) xs<br><br></div>In my opinion, the second one without the case expression is clearer.<br><br></div>One more use case of case expression (and pattern matching) is when you start creating your own datatypes. Then you can destructure the type and add the required logic based on the type:<br><br>data MyType a b = TypeA a | TypeB b<br>                deriving (Show)<br><br>which_type a = case a of<br>  TypeA a -> "Type A" -- Or some better logic here.<br>  TypeB b -> "Type B"<br><br></div><div>Hope this makes things a bit clearer. <br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Thu, Jul 7, 2016 at 6:46 AM, Semih Masat <span dir="ltr"><<a href="mailto:masat.semih@gmail.com" target="_blank">masat.semih@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">Sorry, if i am flooding.<br><br>To make it clear what i wanted to say in last section on previous mail.<br><br><br><br>Lets say i have a list of numbers and i want to do different things in case of different even numbers on that list.<div><br></div><div>If i use guards i will do it like this :</div><div><br></div><div><div>nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]</div><div>howManyEvens = length(removeOdd(nums))</div><div><br></div><div>isItOk count</div><div>    | count > 10 = "Too much"</div><div>    | count > 8   = "Isn't this a little much?"</div><div>    | count > 5   = "I think this is ok"</div><div>    | count > 3   = "Little more please"</div><div>    | count > 0   = "Ooo, cmon"</div><div>    | otherwise   = "We gonna die"</div><div><br></div><div>result = isItOk howManyEvens</div></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br>This is a very stupid example but this will work i guess.<br><br>And if i wanted to this with <b>case</b> , i will do it like;</span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div><div style="font-size:12.8px">isItOk' nums = case (length(removeOdd(nums))) of</div><div style="font-size:12.8px">    10  -> "Too much"</div><div style="font-size:12.8px">    8   -> "Isn't this a little much?"</div><div style="font-size:12.8px">    5   -> "I think this is ok"</div><div style="font-size:12.8px">    3   -> "Little more please"</div><div style="font-size:12.8px">    0   -> "Ooo, cmon"</div><div style="font-size:12.8px">    x   -> "i don't even"</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">So the only different thing is i didn't need to create <span style="font-size:small"><b>howManyEvens</b></span><span style="font-size:small"><b> </b>constant.</span></div><div style="font-size:12.8px"><span style="font-size:small"><br></span></div><div style="font-size:12.8px"><span style="font-size:small"><br></span></div><div style="font-size:12.8px"><span style="font-size:small">PS: While i writing this. I realized that with case, i need to use pattern matching but with guards i can use other functions if i wanted to. ( like count > 10 )</span></div><div>Sorry for asking prematurely. And if anyone reaches this email by google search. Look at this explanation : <a href="http://stackoverflow.com/a/4156831" target="_blank">http://stackoverflow.com/a/4156831</a></div></div><div><br></div><div>To the authors : Please, if you writing a book a blog post about haskell. Don't create same function in different styles. We don't understand which one we need to use and why we have all different choices. </div><div><br></div><div>Thanks.</div><span><font color="#888888"><div>Semih</div></font></span><div><div><div><br></div><div class="gmail_extra"><div class="gmail_quote">On Thu, Jul 7, 2016 at 3:43 AM, Semih Masat <span dir="ltr"><<a href="mailto:masat.semih@gmail.com" target="_blank">masat.semih@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>I am new to Haskell and trying to learn it with <a href="http://learnyouahaskell.com" target="_blank">learnyouahaskell.com</a> and Pluralsight Haskell course. </div><div><br></div><div>And i have a very noob question.</div><div><br></div><div>I understand that <b>if .. else</b> is just a syntactic sugar over <b>case. </b>But what about guards then ?</div><div><br></div><div>Are guards also <b>case </b>in different syntax ? Or vice versa ? Like with an example.</div><div><br></div><div><br></div><div><div>anyEven nums</div><div>    | (length (removeOdd nums)) > 0 = True</div><div>    | otherwise                     = False</div><div><br></div><div><br></div><div>anyEven' nums = case (removeOdd nums) of</div><div>    []        -> False</div><div>    (x:xs)  -> True</div></div><div><br></div><div>I can do the same thing with both of them. </div><div><br></div><div>As i understand the only different thing is, with <b>case </b>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. </div><div>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 )</div><div><br></div><div>Is this it? </div><div><br></div><div>Is this the only difference between them ?</div><div><br></div><div>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 ?</div><div><br></div><div><br></div><div>Thanks, and sorry if my english is bad.</div><span><font color="#888888"><div><br></div><div>Semih Masat</div></font></span></div>
</blockquote></div><br></div></div></div></div>
<br></div></div><span class="">_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></span></blockquote></div><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>