7

As far as I understand map in Haskell takes a function and a List and applies that function to every element in that list before creating a new list with the function applied to each member.

Trying it out, this works fine with really simple functions like (+5) so it works fine if I type:

map (+7) [2,8,9,3]

I get:

[9,15,16,10]

However this doesn't work with functions that I have created. Say I have made a function shiftLetter whose type declaration is:

shiftLetter :: Char -> Int -> Char

(with Int shifting how far along the letter returned is)

If I make a new function to do it with more than one letter and type:

shiftLetters :: String -> Int -> Char
shiftLetters letters shift = map shiftLetters "AKHLKHN"

I get errors, why is this?

9
  • 2
    suppose that you've messed up shiftLetters and shiftAlphabet, check them again Commented Oct 12, 2013 at 20:33
  • What is shiftAlphabet? Something like shiftAlphabet char = shiftLetter char shift might be what you're looking for? Commented Oct 12, 2013 at 20:34
  • 1
    in the interpreter, do these things and figure it out on your own: :t map, :t shiftLetter. Commented Oct 12, 2013 at 20:35
  • and also :t map shiftLetter "AKHLKHN", i mean. Commented Oct 12, 2013 at 20:44
  • 5
    "I get errors, why is this?" because there are errors. And believe it or not, the text of the error messages is of concern, yes, indeed. If you do not read them, you should at least post them. Commented Oct 12, 2013 at 21:41

2 Answers 2

12

If you check the type of the map function then you see:

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

So you need a function that takes one parameter, but yours takes two. You have to use

shiftLetters letters shift = map (flip shiftLetter $ shift) letters

or

shiftLetters letters shift = map (`shiftLetter` shift) letters

Pointless style:

shiftLetters = flip $ map . flip shiftLetter
Sign up to request clarification or add additional context in comments.

7 Comments

Okay thanks but I've never seen flip or $ before? What are they for?
or you can do (`shiftLetter` shift) instead of (flip shiftLetter $ shift)
The type system doesn't prevent you from mapping functions of more than one variable. (In fact, in the 'applicative' style it's very common.) map shiftLetters is a perfectly valid expression, with a type of String -> [(Int -> Char)].
Trying this I get: Syntax error on 'shiftLetter' Perhaps you intended to use -XTemplateHaskell
@Eddie: The space after $ is important.
|
0

It looks like you want

shiftLetters letters shift = map (`shiftLetter` letters) "AKHLKHN"

3 Comments

Because shiftLetters takes a String then an Int?
shiftLetter takes a Char, not a String. Note that String is the same as [Char] which is different from Char.
I know! Sorry the functions have very similar names. shiftLetter takes a Char but shiftLetters takes a String?

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.