Haskell errors: “lacks an accompanying binding” and “not in scope”

前端 未结 2 465
长情又很酷
长情又很酷 2021-01-05 00:26

I have created a piece of code this:

intToDigit :: Char -> Int
ord :: Char -> Int
intToDigit c = ord c - ord \'a\'

However, when I ru

2条回答
  •  失恋的感觉
    2021-01-05 00:49

    Remove the line:

    ord :: Char -> Int  
    

    Or give it a definition.

    And it's a bad idea to name your function intToDigit, while it's already used in Data.Char to do the opposite of what you are doing.

    Your function is Data.Char.digitToInt, and its implementation also works with hexadecimal:

    digitToInt :: Char -> Int
    digitToInt c
     | isDigit c            =  ord c - ord '0'
     | c >= 'a' && c <= 'f' =  ord c - ord 'a' + 10
     | c >= 'A' && c <= 'F' =  ord c - ord 'A' + 10
     | otherwise            =  error ("Char.digitToInt: not a digit " ++ show c) -- sigh
    

    Actually it's not what you defined... why 'a' in your code?

提交回复
热议问题