vendredi 23 octobre 2015

"For all" statements in Haskell

I'm building comfort going through some Haskell toy problems and I've written the following speck of code

multipOf :: [a] -> (Int, a)
multipOf x = (length x, head x)
gmcompress x = (map multipOf).group $ x

which successfully preforms the following operation

gmcompress [1,1,1,1,2,2,2,3] = [(4,1),(3,2),(1,3)]

Now I want this function to instead of telling me that an element of the set had multiplicity 1, to just leave it alone. So to give the result [(4,1),(3,2),3] instead. It be great if there were a way to say (either during or after turning the list into one of pairs) for all elements of multiplicity 1, leave as just an element; else, pair. My initial, naive, thought was to do the following.

multipOf :: [a] -> (Int, a)
multipOf x = if length x = 1 then head x else (length x, head x)
gmcompress x = (map multipOf).group $ x

BUT this doesn't work. I think because the then and else clauses have different types, and unfortunately you can't piece-wise define the (co)domain of your functions. How might I go about getting past this issue?

Aucun commentaire:

Enregistrer un commentaire