2

I have tried to write a function that takes a list pair, and swaps the pair elements

inverse :: [(a,b)] -> [(b,a)]
inverse [] = []
inverse (x,y):xs = (y:x): inverse xs

I have loaded this function via Prelude, it gives me following error:

mydefs.hs:11:1: Parse error in pattern: inverse

This is line 11, inverse (x,y):xs = (y:x): inverse xs

3
  • 1
    colon in (y:x) to (y,x)... Commented Jan 11, 2015 at 9:23
  • A better way of doing this would be making a function swap (a,b) = (b,a) and making inverse = map swap. Commented Jan 11, 2015 at 10:32
  • Possible duplicate of Haskell: Parse error in pattern Commented Sep 6, 2017 at 8:02

1 Answer 1

6

You just have to surround the unpacked tuple and the rest of the list, like this

inverse ((x, y):xs) = (y, x) : inverse xs

Apart from this, you can use the Data.Tuple package's swap function, like this

Prelude> import Data.Tuple
Prelude Data.Tuple> map swap [(1, 2), (3, 4)]
[(2,1),(4,3)]
Sign up to request clarification or add additional context in comments.

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.