× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Cons and ++

Feb 04, 2016
<p>We call the : operator cons. It takes two arguments an item and a list and, importantly it returns a list, so you can chain elements to the head of a list using :</p> <p>Hence the following are equivalent:</p> <p>1:2:3:[] == [1,2,3]</p> <p>[1,2,3] ++ [] == [1,2,3]</p> <p>[1,2] ++ [3] == [1,2,3]</p> <p>1:[2,3] == [1,2,3]</p> <p> </p> <p>Note that : can be used in patterns, but ++ cannot. So you can write a function like:</p> <p>hasAtLeastThree (x:y:z:rest) = True</p> <p>hasAtLeastThree _ = False</p> <p> </p>
comments powered by Disqus