10

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
2
  • Dont forget to accept an answer to your question =) (by clicking the green check icon on the answer) Commented Oct 6, 2012 at 20:22
  • Does the code you listed here give you any error messages? If so, what are they? If not, then what are you asking, exactly? The only error I see is that you used width when you might have meant length? Commented Oct 6, 2012 at 22:33

4 Answers 4

16

Check this out!

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

etc.

Sign up to request clarification or add additional context in comments.

1 Comment

@AndrewC, yes, this isn't a pedagogically helpful answer and I probably wouldn't like it very much if I hadn't written it...
7

Could just use currying and another call to map.

map (map change) $ itrCol xs

To learn more about currying take a look at that chapter in Learn You A Haskell, a great beginner book in Haskell.

3 Comments

Thanks for your response! I'm new to currying, but from my research, it is changing the number of arguments that a function takes?
The best explanation I've read is Learn You A Haskell. You really should read this whole book, wont find anything better!
Thank you! This is my first functional programming language, and it's quite different from the imperative languages I'm used to. I will definitely look into this!
3

map (and fmap more importantly) essentially lifts a function to work on lists, giving you a new function: (I added superfluous parens to make it more clear)

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

If you map that second function ([a] -> [b]) you will get a function that works on lists of lists:

evalChange xs = map (map change) $ itrCol xs

(if this is not what you wanted then please clarify)

3 Comments

Thank you for your response! For some reason, this gives me an error. I tried to include the function type for map but it still gives me an error. I'm not sure how to copy that error in GHC onto here?
@user1670032 copy&paste it as an edit to your answer. If you don't know how then screenshot it.
@user1670032 If you are using ghci in Windows, you need to click on the icon at the top left corner then select "Edit -> Mark" from the menu. Now highlight what you want to copy and then push Enter. From there, just paste as usual wherever you wish.
1

The type signature of map is:

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

One sensible type signature for change is:

change :: [Integer] -> [Integer]

Now map expects a function from a to b as its first argument. If we give it change, a function from [Integer] to [Integer], then a = [Integer] and b = [Integer].

map change :: [[Integer]] -> [[Integer]]

Now if that list comprehension produced from iterCol xs supplies a [[Integer]], then we can apply that to map change:

map change (itrCol xs) :: [[Integer]]

This all looks fine to me. It works because map is polymorphic. If you give it a function that turns A into B, then it will give you back a function that turns lists of A into lists of B. It doesn't matter what A and B are: as you can see here, they can even be lists themselves!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.