I'm a Haskell newbie, trying to accomplish a Caesar cipher exercise.
In a .hs
file, I defined the following function:
let2int :: Char -> Int
let2int c = ord c - ord 'a'
Then I attempt to load this into GHCi by typing :l caeser.hs
and I get the following error message:
[1 of 1] Compiling Main ( caeser.hs, interpreted )
caeser.hs:2:12: Not in scope: `ord'
caeser.hs:2:20: Not in scope: `ord'
From the book I was using, I had the impression that ord
and chr
were standard functions for converting between characters and integers, but it seems evident that I need to "import" them or something. How is this done?
They are standard functions but you need to import them from the right module first. Add
import Data.Char
to ceaser.hs and it should work.
See also http://www.haskell.org/ghc/docs/latest/html/libraries/index.html for the full set of libraries that ship with the compiler.
In "Haskell 2010", ord
lives in Data.Char
So you'll want import Data.Char
or import Data.Char (ord)
In "Haskell 98", ord
can be found in the module Char
.
A great tool for finding functions and their modules is
http://www.haskell.org/hoogle/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With