I want to know if it is possible to convert a recursive function into point free definition.
If we take an easy recursive function.
factorial :: int-> int
factorial 0=1
factorial n+1= (n+1) *factorial n
IF we have non recursive def.
factorial :: int-> int
factorial n= product [1..n]
<=> factorial n = product.enumFromTo 1 n
<=> factorial = product.enumFromTo 1
But how can i do the same on the recursive definition?
Reason why i am asking is that i want to make transformationsApply pointfree.
transformationsApply :: Eq a => a -> ([a] -> [a]) -> [([a], [a])] -> [a] -> Maybe [a]
transformationsApply _ _ [] _= Nothing
transformationsApply wc func ((a,b):xs) (y:ys)
= orElse (transformationApply wc func (y:ys) (a,b))
(transformationsApply wc func xs (y:ys))
transformationApply used above is defined as
transformationApply :: Eq a => a -> (([a] -> [a]) -> ([a] -> (([a], [a]) -> Maybe [a])))
transformationApply wc func xs (a,b)
= mmap ((substitute wc b).func) (match wc a xs)
I would suggest to keep your code more readable than to try converting it to incomprehensible pointfree form.
The easiest way to convert a function to pointfree form is to ask lambdabot. Now when you have recursive function you can convert that to non-recursive using fix. Here is the example of fact function (A direct conversion from what lambdabot gave) and you can see how readable it is.
import Control.Monad
import Data.Function
if' :: Bool -> a -> a -> a
if' True x _ = x
if' False _ y = y
fact = fix $ ap (flip if' 1 . (0 ==)) . ap (*) . (. subtract 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With