Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Functional > Re: pointfree n...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 6 Topic 2804 of 3037
Post > Topic >>

Re: pointfree notation

by Chris Smith <cdsmith@[EMAIL PROTECTED] > Apr 2, 2008 at 01:53 PM

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.)
 




 6 Posts in Topic:
pointfree notation
guthrie <guthrie@[EMAI  2008-03-31 09:59:19 
Re: pointfree notation
guthrie <guthrie@[EMAI  2008-03-31 10:27:18 
Re: pointfree notation
Paul Rubin <http://phr  2008-03-31 10:45:01 
Re: pointfree notation
Chris Smith <cdsmith@[  2008-03-31 17:57:22 
Re: pointfree notation
KE <koray.erkan@[EMAIL  2008-04-02 04:19:28 
Re: pointfree notation
Chris Smith <cdsmith@[  2008-04-02 13:53:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Oct 10 13:13:09 CDT 2008.