开发者

Define a function without using the fact that it is in Show class

Not using the fact that the Integer type belongs to the Show class define a function

integerTostring :: Integer -> String

Hi开发者_JAVA百科nt: use the function

unfoldr :: (b-> Maybe(a,b))->b->[a])

from the List module, defined by the formula

unfoldr f b=
            case f b of
                  Nothing -> []
                  Just (a,b) -> a : unfoldr f b



where
**data Maybe a = Nothing| Just a deriving (Eq, Ord, Read, Show)**

The Char module exports the function

intToDigit :: Int -> Char

the standard prelude offers the following method of the class Enum:

fromEnum :: (Enum a) => a -> Int

and the Integer type belongs to the Enum class...

What I did is:

   import List, Char
    integerToString :: Integer -> String
    integerToString = reverse.unfoldr(\a -> if a>0 then Just(IntToDigit$fromEnum$a'mod'10,a'div'10)else Nothing

Is that what i had to do? if not then what to do ?


Yeah, that's a good solution, although you forgot to handle negative integers and 0. Also, I suggest using the divMod function instead of using div and mod seperately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜