Question

I'm trying to change

function (x:xs) = unwords (map reverse (words (x:xs)))

into point free style, and I can't do this.

isn't it simply

function = unwords . map . reverse . words

Please help me to change this to point free style.

I tried to use pointfree after doing below command line

cabal install pointfree

Everything compiled and installed but when I type pointfree in terminal, it's keep saying there's no such thing.

Was it helpful?

Solution

Your definition is almost correct and can be tested in ghci interpreter, it should be

ghci> let function = unwords . map reverse . words
ghci> function "The quick brown fox"
"ehT kciuq nworb xof"

because reverse is an argument of map. Basically, to change pipeline-like function foo a3 = f a1 (g a2 (h a3)) (consisting of nested function calls) into pointfree, where the explicit argument (a3 in this case) is the last called, you need to remove it and replace nested parentheses with . composition operator: foo = f a1 . g a2 . h. However, this trick doesn't work when there are multiple arguments or the argument is used more than once. To meaningly operate with pointfree functions, you should understand combinatory logic.

EDIT: oh, I got that you tried to use the command-line pointfree tool to auto-transform the function. Binaries compiled with cabal are placed in specific directory (for example, C:\Users\Username\AppData\Roaming\cabal\bin in Windows, AFAIR) which is probably isn't added to PATH by default and so command line interpreter can't find the executable.

OTHER TIPS

reverse is an argument of map, so you don't have to compose map with reverse, but you want to compose the full map reverse with the other functions.

The correct way to do so is:

function = unwords . map reverse . words
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top