Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Map function onto a list of lists

How do you map a function to operate on lists within a list? The following is just what I'm trying to do as an example, but I was just asking as a general question. Thanks in advance!

Right now, I'm trying to map a function, change, onto each lists of one list (returned by itrCol xs).

evalChange xs = map change $ itrCol xs

where itrCol returns a list of lists, where each containing list is a column.

itrCol xs = [getCol x xs | x <- (take (width xs) (iterate (\x -> (x + 1)*1) 0))]

getCol lists column given list of column indices

getCol :: Int -> [t] -> [t]

and change is:

change []     = []
change [x]    = [x]
change [x,y]  = [x,y]
change (x:y:z:ws) | x == y && y == z = 0 : y*(-1) : 0 : change ws
change (x:xs) =  x : change xs
like image 718
user1670032 Avatar asked Oct 06 '12 19:10

user1670032


People also ask

What does map function do in Haskell?

map takes a function and a list and applies that function to every element in the list, producing a new list.

How do you get the first N elements of a list in Haskell?

There is a function in Haskell that takes first n elements of user-supplied list, named take . The syntax is: function-name arg1 arg2 . So, take takes first 1000 elements from an infinite list of numbers from 0 to infinity.

Which operator is used to get an element out of a list by index in Haskell?

The straight answer was already given: Use !! . However newbies often tend to overuse this operator, which is expensive in Haskell (because you work on single linked lists, not on arrays).


1 Answers

Check this out!

map           :: (a -> b) ->   [a]   ->   [b]
(map.map)     :: (a -> b) ->  [[a]]  ->  [[b]]
(map.map.map) :: (a -> b) -> [[[a]]] -> [[[b]]]

etc.

like image 68
luqui Avatar answered Nov 05 '22 21:11

luqui