Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointfree style programming in haskell

I have this function

rulesApply :: [PhrasePair] -> Phrase ->  Phrase

rulesApply pp = try (transformationsApply "*" reflect  pp )

I want to learn how to make it pointfree.

try :: (a -> Maybe a) -> a -> a
try f x = maybe x id (f x)
transformationsApply :: Eq a => a -> ([a] -> [a]) -> ([([a], [a])] -> ([a] -> Maybe [a]))
transformationsApply wc f pfPair list = foldr1 orElse (map (transformationApply wc f list) pfPair)


 rulesApply pp = try (transformationsApply "*" reflect  pp )

(transformationsApply "*" reflect ) pp has type Eq a => ([([a], [a])] -> ([a] -> Maybe [a]))

We see that

try :: (a -> Maybe a) -> a -> a

so try takes a function (a -> Maybe a) as argument. and we see that return type of (transformationsApply "*" reflect ) pp is ([a] -> Maybe [a])) so we should be able to write.

rulesApply pp = try . (transformationsApply "*" reflect) pp

but this gives compilation error.

like image 916
user2975699 Avatar asked Feb 20 '26 09:02

user2975699


1 Answers

Whenever you have something that looks like

\x -> f (g x)

you can turn it into

f . g

In this case, you have

s          x  = f   (g                                 x  )
rulesApply pp = try (transformationsApply "*" reflect  pp )

which can be transformed (by moving the argument to the other side of the equation) into

s          = \x  -> f   (g                                 x  )
rulesApply = \pp -> try (transformationsApply "*" reflect  pp )

which in turn is, according to our rule,

s          = f   . g
rulesApply = try . transformationsApply "*" reflect
like image 56
kqr Avatar answered Feb 23 '26 02:02

kqr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!