Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a Haskell library function?

Tags:

haskell

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?

like image 804
Eric Wilson Avatar asked Nov 22 '10 19:11

Eric Wilson


2 Answers

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.

like image 155
Paul Johnson Avatar answered Oct 13 '22 13:10

Paul Johnson


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/

like image 40
wnoise Avatar answered Oct 13 '22 13:10

wnoise