Euro Pe
Euro Pe

Reputation: 73

Swap function in Haskell

I'm starting my adventure with Haskell language and I need a little help. How should I define swap function which is declared:

swap :: (Int, Char) -> (Char, Int)

Probably it's very easy, but I have problem with that.

Upvotes: 2

Views: 8317

Answers (2)

lamg
lamg

Reputation: 364

You can also be more general with:

swap :: (a, b) -> (b, a)
swap (x, y) = (y, x)

This works for all types, not just for Int and Char.

Upvotes: 9

Chad Gilbert
Chad Gilbert

Reputation: 36375

swap :: (Int, Char) -> (Char, Int)
swap (a, b) = (b, a)

You might want to read up on pattern matching in Haskell.

Upvotes: 7

Related Questions