
I don't know if the following explanation seems too basic or redundant to you, I'm sorry if that is the case, but it might help if you don't understand high order functions very well: In my description of the declarative meaning of the definition I understood this: "map (x:) (cinits xs)" as add the element 'x' in the front of every element (int this case, the elements are lists) of the list returned by (cinit xs) Thing of (:) as a function: (:) :: a -> [a] -> [a] That appends its first argument in the front of the list given as a second argument. Then (x:) is a function with type: (x:) :: [a] -> [a] It is a function that appends to a fixed element x to a given list. As "map f list" returns a new list that is the result of applying f to every element of "list", assuming "list" is a list of lists, "map (x:) list" returns a new list that has 'x' in the front of every element. An example: map (1:) [[2], [3], [4]] Prelude> map (1:) [[2], [3], [4]] [[1,2],[1,3],[1,4]] Because: map f [[2], [3], [4]] = [f [2], f [3], f [4]] So let f = (x:) then: map f [[2], [3], [4]] = [(1:) [2], (1:) [3], (1:) [4]] = [[1, 2], [1, 3], [1, 4]] JP Juan Pedro Bolivar Puente wrote:
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe