I have a graph of Function f(n) where it returns
5 if n = 0
2 if n = 1
-4 if n = 2
1 if n = 3
9 if n = 4
8 if n = 5
9 if n = 6
0 otherwise
and I wanted to write a function that will represent a graph with one List with pairs:
type Nat0 = Int
type Z = Int
type List = [Z]
type Graph = [(Nat0,Z)]
list_to_graph :: List -> Graph
list_to_graph x = list_to_graph' ([0..(length x)-1 ]) (x)
list_to_graph' :: [Int] -> List -> Graph
list_to_graph' (x:xs) (y:ys) = [(x, y)] ++ list_to_graph' (xs) (ys)
list_to_graph' [] [] = []
And thats what I did here. Passing a list [5,2,-4,1,9,8,9] returns
*Main> list_to_graph [5,2,-4,1,9,8,9]
[(0,5),(1,2),(2,-4),(3,1),(4,9),(5,8),(6,9)]
and here is the function that does oposite:
graph_to_list :: Graph -> List
graph_to_list (x:xs) = [snd (x)] ++ graph_to_list(xs)
graph_to_list []= []
where passing the graph [(0,5),(1,2),(2,-4),(3,1),(4,9),(5,8),(6,9)]
*Main> graph_to_list [(0,5),(1,2),(2,-4),(3,1),(4,9),(5,8),(6,9)]
[5,2,-4,1,9,8,9]
Question:
What I dont understand is how to write something like this:
type Function = (Nat0 -> Z)
function_to_list :: Function -> List
or
list_to_function :: List -> Function
or the same for graph
function_to_graph :: Function -> Graph
graph_to_function :: Graph -> Function
I have read Higher order functions on this link but I cant seem to grasp how this actually works.
I suppose in function_to_list I would have to pass a function that has this notation (Nat0 -> Z)(which is actually Int -> Int ) and it should return a List with [Z] (which is [Int] ). But how do I do that? That is like passing same function to itself?
Even more confusing is the list_to_function what should here be the result?
If somebody can explain me higher order Functions on some of my examples I would really be thankful!
EDIT:
To be more clear here is what I want to achieve:
(list_to_graph . graph_to_list) = λ x. x
(graph_to_list . list_to_graph) = λ x. x
As I showed above if I pass a list to list_to_graph it return a graph and graph_to_list does oposite
(list_to_function . function_to_list) = λ x. x
(function_to_list . list_to_function) = λ x. x
its the same that I want to do with the other two functions. If I apply function_to_list to list_to_function as function_to_list returns a List and list_to_function accepts a List it should return a function that will take a elements out of the list and apply it to Function which will return a Z.
What I figured out by now:
function_to_list :: Function-> List
function_to_list f = [f(x) | x <- [0..6]]
function :: Function
function n
| n == 0 = 5
| n == 1 = 2
| n == 2 = (-4)
| n == 3 = 1
| n == 4 = 9
| n == 5 = 8
| n == 6 = 9
| otherwise = 0
As the answer bellow suggested.
*Main> function_to_list function
[5,2,-4,1,9,8,9]
What I want to do is to make this function :: Function in my list_to_function
function. but it is impossible, sincefunction 4 = function 6 = 9, not one to one or injective function.