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