KE wrote:
> Anyway, assuming we have the definition of "numOccurences" as
>
> numOccurences :: Eq a => a -> [a] -> Int numOccurences = (length
> .) . filter . (==)
>
> ...how do we use it to write a one-liner that maps it to a range of
> chars representing the alphabet (say "['a'..'z']") to check the string
> "supercalifragilisticexpialidocious" and return a list of the
> occurrences of all alphabet letters? (Can/should we do it using "map"?).
> How do we write a "pointfree" version of this?
That would be
map (flip numOccurrences "supercala...") ['a'..'z']
> Also, here's another function:
>
> subElement xs f p i = ((filter p . map f) xs)!! i
>
> How would this be rendered "pointfree?"
In general, if you want to know how to write a pointfree function, you
can ask lambdabot. The easiest way is to grab an IRC client, go to
irc.freenode.net, and type
/msg lambdabot @[EMAIL PROTECTED]
subElement xs f p i = ((filter p . map f) xs)!! i
You can also install lambdabot locally, if you use it a lot; though the
last time I looked, Linux was a definite prerequisite for that.
In this case the answer is:
subElement = (((!!) .) .) . flip (flip . flip ((.) . filter) . map)
Which is ugly enough that you certainly don't want to write this function
in point-free style. (Unless, of course, you're trying to start a new
obfuscated code contest for Haskell.)


|