How the following works

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

Why don't you hand execute it on an example, like cinits [1,2,3]?
cinits [1,2,3] = cinits (1:2:3:[]) =
[] : map (1:) (cinits (2:3:[]) =
[] : map (1:) ([] : map (2:) (cinits (3:[])) =
...
On Tue, Apr 14, 2009 at 10:39 AM, Tsunkiet Man
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

Let's see, if I execute it by hand: cinits :: [a] -> [[a]] cinits [] = [[]] cinits (x:xs) = [] : map (x:) (cinits xs) cinits [1,2,3] = [] : map (1:) ( [] : map (2:) ( [] : map (3:) ( [[]]) ) ) = [] : map (1:) ( [] : map (2:) ( [] : map (3:) [[]] ) ) = [] : map (1:) ( [] : map (2:) ( [] : [[3]] ) = [] : map (1:) ( [] : map (2:) ( [[], [3]] ) = [] : map (1:) ( [] : [[2], [2,3]]) = [] : map (1:) ( [[], [2], [2,3]]) = [[],[1], [1,2], [1,2,3]] Well, I understand this part. But isn't there an easier way to "see" the answer? Thank you for your help!

Assuming you already think you know what cinits does, you can convince
yourself using induction.
On Tue, Apr 14, 2009 at 11:16 AM, Tsunkiet Man
Let's see, if I execute it by hand:
cinits :: [a] -> [[a]] cinits [] = [[]] cinits (x:xs) = [] : map (x:) (cinits xs)
cinits [1,2,3] = [] : map (1:) ( [] : map (2:) ( [] : map (3:) ( [[]]) ) ) = [] : map (1:) ( [] : map (2:) ( [] : map (3:) [[]] ) ) = [] : map (1:) ( [] : map (2:) ( [] : [[3]] ) = [] : map (1:) ( [] : map (2:) ( [[], [3]] ) = [] : map (1:) ( [] : [[2], [2,3]]) = [] : map (1:) ( [[], [2], [2,3]]) = [[],[1], [1,2], [1,2,3]]
Well, I understand this part. But isn't there an easier way to "see" the answer?
Thank you for your help! _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Am Dienstag 14 April 2009 10:39:28 schrieb Tsunkiet Man:
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?
Perhaps it's easier to follow as a list comprehension: cinits [] = [[]] cinits (hd:tl) = [] : [ hd : rest | rest <- cinits tl ]
(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

Let's see...
cinits [] = [[]]
cinits (hd:tl) = [] : [ hd : rest | rest <- cinits tl ]
Well, ehm, I'm trying to understand "map" in "map" functions, however I do
understand list comprehensions. But I don't think I can write "any" "map" in
"map" function into a list comprehension can I?
2009/4/14 Daniel Fischer
Am Dienstag 14 April 2009 10:39:28 schrieb Tsunkiet Man:
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?
Perhaps it's easier to follow as a list comprehension:
cinits [] = [[]] cinits (hd:tl) = [] : [ hd : rest | rest <- cinits tl ]
(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

map f xs = [ f x | x <- xs ]
On Tue, Apr 14, 2009 at 11:21 AM, Tsunkiet Man
Let's see...
cinits [] = [[]] cinits (hd:tl) = [] : [ hd : rest | rest <- cinits tl ]
Well, ehm, I'm trying to understand "map" in "map" functions, however I do understand list comprehensions. But I don't think I can write "any" "map" in "map" function into a list comprehension can I?
2009/4/14 Daniel Fischer
Am Dienstag 14 April 2009 10:39:28 schrieb Tsunkiet Man:
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?
Perhaps it's easier to follow as a list comprehension:
cinits [] = [[]] cinits (hd:tl) = [] : [ hd : rest | rest <- cinits tl ]
(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

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

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
participants (4)
-
Daniel Fischer
-
Juan Pedro Bolivar Puente
-
Lennart Augustsson
-
Tsunkiet Man