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
map takes a function and a list and applies that function to every element in the list, producing a new list.
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.
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).
Check this out!
map :: (a -> b) -> [a] -> [b]
(map.map) :: (a -> b) -> [[a]] -> [[b]]
(map.map.map) :: (a -> b) -> [[[a]]] -> [[[b]]]
etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With