Haskell errors: "lacks an accompanying binding" and "not in scope"
Solution 1
You need to provide an implementation for the function ord
. Here, you have given a signature for ord
, but no implementation.
Or you can use Haskell's own ord
function, that is Char.ord
.
Solution 2
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?
Related videos on Youtube
anon1
Updated on July 09, 2022Comments
-
anon1 almost 2 years
I have created a piece of code this:
intToDigit :: Char -> Int ord :: Char -> Int intToDigit c = ord c - ord 'a'
However, when I run it I get this error message:
ChangeVowels.hs:2:1: The type signature for `ord' lacks an accompanying binding
ChangeVowels.hs:4:16: Not in scope: `ord'
ChangeVowels.hs:4:24: Not in scope: `ord'
I tried it with
Import data.char
but that doesnt work either.-
dave4420 about 13 yearsNote that capitalisation is important: your import line should be
import Data.Char
(or perhapsimport Data.Char hiding (intToDigit)
). -
Dan Burton about 13 years@Dave or better,
import Data.Char (ord)
-
v0d1ch over 7 yearsAlso if you incidentally lowercase your function name so you have intToDigit in type declaration and inttodigit in implementation you will get lacks an accompanying yada yada. I learned the hard way...
-