Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse an integer in haskell?

I need help on how to reverse a Integer in Haskell with the following type signature:

reverseInt :: Integer -> Integer
reverseInt a = undefined -- help here

I need the Integer input number to be reversed like the example below.

Example:

> reverseInt 1989
9891
like image 845
Noisie Avatar asked Nov 28 '22 13:11

Noisie


1 Answers

reverseInt :: Integer -> Integer
reverseInt = read . reverse . show

This does not address negative numbers. If you need to reverse negative numbers, you can simply use

reverseInt :: Integer -> Integer
reverseInt x = (*) (signum x) . read . reverse . show . abs  $ x
like image 125
Zeta Avatar answered Dec 04 '22 11:12

Zeta