A question on pointfree programming

9 views
Skip to first unread message

TJ

unread,
Dec 21, 2011, 3:12:42 AM12/21/11
to pdxfunc
"Learn You a Haskell for Great Good!" has a great example to add
polymials: ...the polynomials 3x2 + 5x + 9, 10x3 + 9 and 8x3 + 5x2 + x
- 1 and we
want to add them together...

ghci> map sum $ transpose [[0,3,5,9],[10,0,0,9],[8,5,1,-1]]
[18,8,6,17]

If I change it pointfree as below, then it fails on GHCi version
7.0.4:

import qualified Data.List as L
polyadd :: Num a => [[a]] -> [a]
polyadd = map sum $ L.transpose

ghci> :l list_demo
list_demo.hs:7:21:
Couldn't match expected type `[[b0]]'
with actual type `[[a0]] -> [[a0]]'
In the second argument of `($)', namely `L.transpose'
In the expression: map sum $ L.transpose
In an equation for `polyadd': polyadd = map sum $ L.transpose

Please a kind soul tell me why so.
-- An alternate pointfree program based on (.) works fine:
polyadd = (map sum) . L.transpose
-- So does a pointful program
polyadd x = map sum $ L.transpose x

Thanks,
TJ

Shachaf Ben-Kiki

unread,
Dec 21, 2011, 3:20:31 AM12/21/11
to pdx...@googlegroups.com

"map sum $ L.transpose" is the same as "map sum L.transpose" -- i.e.,
it calls map with the arguments "sum" and "L.transpose". map's second
argument is a list; L.transpose isn't. (.) and ($) are not
interchangeable in most cases.

Shachaf

(By the way, if you like having this sort of small question answered
fairly quickly, #haskell on irc.freenode.net is often a good place to
ask.)

Nathan Collins

unread,
Dec 21, 2011, 5:16:41 AM12/21/11
to pdx...@googlegroups.com

And in fact, the point-less plugin [1] for lambdabot can answer many
of these "how do I write 'e' in pointfree form?" questions. You enter
'@pl e' in #haskell to get a point-free version of 'e'. E.g.

<you> @pl \xs -> map sum $ transpose xs
<lambdabot> map sum . transpose

Cheers,

-nathan

[1] http://www.haskell.org/haskellwiki/Pointfree#Tool_support

Reply all
Reply to author
Forward
0 new messages