<div dir="ltr"><div>Hí everybody, <br></div><div><br></div><div>Earlier today I was testing some code in ghci with ":set +s" enabled. For some reason I changed an if-then-else expressions to guards and I was surprised to find out that the memory usage declined significantly (around 20%). <br></div><div><br></div><div>Here is a function call output using if-then-else:</div><div>λ> inarow1 ls<br>1<br>(1.28 secs, 401,690,400 bytes)</div><div></div><div><br></div><div>And here is a function call output using guards: <br></div><div>λ> inarow1 ls<br>1<br>(1.18 secs, 313,690,576 bytes)</div><div></div><div><br></div><div>I ran both versions many times and this difference was consistent. Could you help me understand why?</div><div><br></div><div>Thanks</div><div><br></div><div>Miguel<br></div><div><br></div><div>P.S. I don't think it should matter but maybe it does, here is the function definition:</div><div><br></div><div>inarow1 :: forall a. Eq a => [a] -> Int<br>inarow1 []     = 0<br>inarow1 (x:[]) = 1<br>inarow1 ls     = aux 0 1 ls where<br>  aux :: Int -> Int -> [a] -> Int<br>  aux top curr (x:y:[])      = max top $ if x == y then curr + 1 else curr<br>  aux top curr (x:xs@(y:ys))<br>    | x == y    = aux top (curr + 1) xs<br>    | otherwise = aux (max top curr) 1 xs</div><div><br></div><div>And the list:</div><div>ls = [1..1000000]</div></div>