[TxMt] Haskell?

William D. Neumann wneumann at cs.unm.edu
Tue Nov 1 20:23:55 UTC 2005


On Tue, 1 Nov 2005, Matt Mower wrote:

> So far I can't actually get any code to run in the GHC interpreter,
> something about my functions not being in scope... a most confusing
> start, worse even than Lisp :-)

Yeah... one of the great problems with Haskell is the relative paucity of 
"Haskell for n00bs" type documentation.  A couple of good sources are
Two Dozen Short Lessons in Haskell [1], and Yet Another Haskell Tutorial 
[2] (note that Two Dozen uses hugs, not ghci).

[1] http://www.cs.ou.edu/cs1323h/textbook/haskell.shtml
[2] http://www.isi.edu/~hdaume/htut/

Your problem seems to be that either you haven't loaded the module(s) 
containing your functions, or you've loaded multiple modules and, in the 
process, hid the modules with the functions you wanted.  There are a 
couple of ways to fix this.  Say you have the following two files:

------
Pa.hs:
module Pa where

parallel_apply [] _ _ = []
parallel_apply (f:fs) xs ys = (zipWith f xs ys):(parallel_apply fs xs ys)

Ftest.hs:
module Ftest where

fact 0 = 1
fact x = x * fact (pred x)
-----

Now, how can you use the functions fact and parallel_apply in ghci?
Well, one way is to pass them to ghci on the command line (note, we will 
be assuming that we launch ghci from the directory containing Pa.hs and 
Ftest.hs):

[228] 12:51PM% ghci Pa ftest

This tells ghci to load our code and make it available.  We are 
(eventually) met with the message and prompt:
Loading package base-1.0 ... linking ... done.
Compiling Pa               ( Pa.hs, interpreted )
Compiling Ftest            ( ftest.hs, interpreted )
Ok, modules loaded: Ftest, Pa.
*Ftest>

This tells us that the modules Pa and Ftest were loaded, and the prompt 
tells us that the module Ftest is at the top level, i.e. all of its 
defenitions are in scope.  Let's try it out:

*Ftest> fact 10
3628800

So far so good.  Now let's try parallel_apply:

*Ftest> parallel_apply [(+), (*)] [1,2,3] [4,5,6]

<interactive>:1:0: Not in scope: `parallel_apply'

Nuts... we can't get at it, because Ftest is hiding it.  We can, however, 
reach it by its qualified name:

*Ftest> Pa.parallel_apply [(+), (*)] [1,2,3] [4,5,6]
[[5,7,9],[4,10,18]]

Note that if the prompt read *Pa>, then the definitions from module Pa 
woult be at the toplevel, and to access fact, we'd need to say, e.g.
Ftest.fact 10.

Now, you can get the same effect if you launch ghci without specifying any 
modules at the command line by using the  :load and :add commands

Prelude> :load Pa
Compiling Pa               ( Pa.hs, interpreted )
Ok, modules loaded: Pa.
*Pa> :add Ftest
Compiling Ftest            ( Ftest.hs, interpreted )
Ok, modules loaded: Ftest, Pa.
*Ftest> fact 10
3628800
*Ftest> parallel_apply [(+), (*)] [1,2,3] [4,5,6]

<interactive>:1:0: Not in scope: `parallel_apply'
*Ftest> Pa.parallel_apply [(+), (*)] [1,2,3] [4,5,6]
[[5,7,9],[4,10,18]]

Here, the last loaded module is the visible one.  You can also load 
modules together:

Prelude> :load Pa Ftest
Compiling Pa               ( Pa.hs, interpreted )
Compiling Ftest            ( Ftest.hs, interpreted )
Ok, modules loaded: Ftest, Pa.
*Ftest> fact 10
3628800
*Ftest> Pa.parallel_apply [(+), (*)] [1,2,3] [4,5,6]
[[5,7,9],[4,10,18]]

And if you want to have them both at the top level, you can create a third 
file and make use of the import keyword:
-----
Pf.hs:
module Pf where

import Ftest
import Pa
-----

Now you can fire up ghci and load the Pf module:
Prelude> :load Pf
Compiling Ftest            ( ./Ftest.hs, interpreted )
Compiling Pa               ( ./Pa.hs, interpreted )
Compiling Pf               ( Pf.hs, interpreted )
Ok, modules loaded: Pf, Pa, Ftest.
*Pf> fact 10
3628800
*Pf> parallel_apply [(+), (*)] [1,2,3] [4,5,6]
[[5,7,9],[4,10,18]]

Or you can use import qualified which requires that you prefix names with 
the name of its original module:
-----
Pf.hs:
module Pf where

import qualified Ftest
import qualified Pa
-----

Prelude> :load Pf
Compiling Ftest            ( ./Ftest.hs, interpreted )
Compiling Pa               ( ./Pa.hs, interpreted )
Compiling Pf               ( Pf.hs, interpreted )
Ok, modules loaded: Pf, Pa, Ftest.
*Pf> fact 10

<interactive>:1:0: Not in scope: `fact'
*Pf> parallel_apply [(+), (*)] [1,2,3] [4,5,6]

<interactive>:1:0: Not in scope: `parallel_apply'
*Pf> Ftest.fact 10
3628800
*Pf> Pa.parallel_apply [(+), (*)] [1,2,3] [4,5,6]
[[5,7,9],[4,10,18]]


Anyway, sorry for going so far off the TextMate topic...

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers.  We don't need them, we're not doing 
anything with them.

Tigers are noble and sleek; children are loud and messy."

         -- Neko Case

Life is unfair.  Kill yourself or get over it.
 	-- Black Box Recorder



More information about the textmate mailing list