It happens to all of us... You're writing some Haskell, minding your own businesses... When all of a sudden,
bam! The looming horns of a dilemma, threatening to impale you: do you use pattern matching to decompose an argument to a function? Or do you decompose it otherwise, so you can refer to the intact argument by name? A tough question, to be sure...
Lets say we want to duplicate the first element of a list. We could do it in one of two ways:
dupHead a = head a : a
dupHead (h:t) = h : h : t
Woah, cowboy! These functions are
way too long. Instead, let's use an 'as-pattern' so we can have our cake and eat it too:
dupHead a@(h:_) = h : a
Cryptorific!
1 comment:
love your haiku from iceland
Post a Comment