定义所需的Haskell实例

时间:2017-03-25 21:35:11

标签: function haskell

我有以下Haskell代码,这个函数检查一个数字中有多少个数字相同,并根据它们中有多少数字相同来获取一个值。我一直收到错误:

  

数字定义所需的(Ord(Int - > Int - > Int),Num(Int - > Int - > Int))实例

digits :: Int->Int->Int 

digits x y
    |(equal < 2) = 0
    |(equal == 2) = 1
    |(equal == 3) = 5
    |(equal == 4) = 20
    |(equal == 5) = 300
    |(equal == 6) = 8000
    |(equal == 7) = 10000
    |otherwise = 100000
    where
        equal :: Int -> Int -> Int

        equal 0 0 = 0
        equal a b
            |(a `mod` 10 == b `mod` 10) = 1 + equal (a `div` 10) (b `div` 10)
            |otherwise = 1 + equal (a `div` 10) (b `div` 10)

我做错了什么?这个错误到底意味着什么呢?

编辑:它是否像这样,它就像一个魅力!

digits :: Int->Int->Int 

digits x y
    |(c < 2) = 0
    |(c == 2) = 1
    |(c == 3) = 5
    |(c == 4) = 20
    |(c == 5) = 300
    |(c == 6) = 8000
    |(c == 7) = 10000
    |otherwise = 100000
    where c = equal x y

        where
            equal :: Int -> Int -> Int

            equal 0 0 = 0
            equal a b
                |(a `mod` 10 == b `mod` 10) = 1 + equal (a `div` 10) (b `div` 10)
                |otherwise = 0 + equal (a `div` 10) (b `div` 10)

0 个答案:

没有答案