[Haskell-cafe] Re: How the following works

Juan Pedro Bolivar Puente magnicida at gmail.com
Thu Apr 16 15:17:16 EDT 2009


I find it easier to understand when looking at the declarative meaning
of the definition. There is the trivial case of the empty list, but for
the non-empty list:

The possible inits of the list is the empty list and the inits of the
"rest of the list" with the "head" element it their front.

The execution would look like this:
1. The recurrsion would get to the tail of the list and calculate its
inits, [[]].
2. Then the recursion would go back, add the (n-th) element at  the
front of all the previously calculated inits [[x_n]], and add the empty
list, we get [[],[x_n]].
3. The same applies for the element n-1 and we get, [x, [x_(n-1)],
[x_(n-1), n]]
...

I hope this helped :p

JP


Tsunkiet Man wrote:
> Hello,
> 
> I can hardly imagine how the following code works:
> 
> cinits :: [a] -> [[a]]
> cinits [] = [[]]
> cinits (x:xs) = [] : map (x:) (cinits xs)
> 
> can someone give me a good explaination?
> 
> (I understand it a bit, but it's really hard for me to figure out how a map
> in a map function works.)
> 
> Thank you for your time,
> 
> Tsunkiet
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list