A Note On Different Relation Of Function

1433 Words6 Pages
Recursions with Folds
Apart from using the recursion directly, fold is available to occur recursion scheme, which applies a function to data structure/Functor to manipulate its contents. Figure 1
The above illustrates the summary of different relation of function. Below we will focus on further explaining of how to isolate the recursions from the code in order to gain the benefit of convenience, which is easier to control, repair and further develop the code.
In (inception) & Fix (fixpoint)
The whole idea if In and Fix is all about where the recursion of code performs in the data type, which isolate the recursion to a single spot, usually represented by parameter k.
First of all, we would have a simple code with direct recursions and
…show more content…
The idea of writing the value of type fix List’ a is shown as below:
As compared with List a and List’ a. we can make a assumption of:
List a ≅ List ‘a k ≅ (Fix (List’ a))

By applying the assumption into Empty and Cons,
Empty’ :: List ‘ a k
Empty’ :: List’a (Fix(List’ a))
Cons’ :: a -> k -> List’ a k

By substituting an integer, 20, into a and Empty’ into k,
Cons’ 20 Empty’ :: List’ Int (List’ a k)

Instead of this presentation, we want to make this in one level deeper via In,
Cons’ 20 9In Empty’) :: List’ Int (Fix (List’a))
In Cons’ 20 (In Empty’) :: Fix (List’a)

Thus, applying this idea into the case, we can conclude that In is a function to construct a new value of type Fix f giving the functor of f.
Data Fix f = In(f (Fix f))--------------1
In :: f(Fix f) -> Fix f Here is another example for using Fix, In to isolate the recursion: data Fix f = In (f (Fix f))

data List ' a k = Empty ' | Cons ' a k toList ' :: [a] -> Fix (List ' a) toList ' [] = In (Empty ') toList ' (x:xs) = In (Cons ' x (toList ' xs))

Applying 1 to this, data Fix List’ [a] = In ((List’a)k) where k is the parameter for recursion size. toList ' = foldr f k where Foldr:
Foldr :; (a->b->b)-> b -> [a] –>b

In order to abstract from f(Fix) to Fix f, k :: Fix (List ' a) k = In (Empty ')

f :: a -> Fix (List ' a) -> Fix (List ' a) f x ys =
Get Access