[Haskell-beginners] question about list processing

Dennis Raddle dennis.raddle at gmail.com
Thu Nov 12 11:45:24 UTC 2015


What would be an elegant way of writing this

computeHead :: (a -> [b]) -> [a] -> [b]

Where when [a] is null, it returns a null list, but when [a] contains one
or more elements, it applies the given function to the head of a and
returns that? Is there some existing typeclass operator that facilitates
this?

You can write

computeHead _ [] = []
computeHead f (x:_) = f x

But that first line seems suspicious to me... it makes me think about how
in the list Monad, an empty list falls through. But I can't quite make it
work.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151112/697a7474/attachment.html>


More information about the Beginners mailing list