0
votes

I'm having trouble with the following Data Type and Function:

module Lib
    (intListProd) where

data IntList = Empty
             | Cons Int IntList
  deriving Show

intListProd :: IntList -> Int
intListProd Empty = 1
intListProd (Cons x xs) = x * intListProd xs

But if I try to use it within ghci, I get errors about 'Cons' and 'Empty' not being in the scope:

*Main Lib> intListProd (Cons 3 (Cons 2 (Cons 4 Empty)))

<interactive>:19:14: Not in scope: data constructor ‘Cons’

<interactive>:19:22: Not in scope: data constructor ‘Cons’

<interactive>:19:30: Not in scope: data constructor ‘Cons’

<interactive>:19:37: Not in scope: data constructor ‘Empty’

I'm using stack, so I use 'stack ghci' to enter the shell.

The code is not my own, I'm trying to follow the examples at the end of chapter 2 in the School of Haskell Introduction to Haskell tutorial by Brent Yorgey.

I've noticed the "Learn you a Haskell" also uses the 'Cons' constructor in the "Recursive Data Types" section. Is 'Cons' something that should be included in GHC that I don't have for some reason?? What about 'Empty'?

2
In the type signature of intListProd, you have Int List. I assume that is meant to be IntList? - Xandaros
Oh, yeah; that wasn't in my code, I typed this by hand. :) Fixed it. - natecornell

2 Answers

3
votes
module Lib
    (intListProd) where

You're not exporting any constructor of IntList. Export them, so that they're available to other modules:

module Lib
    (intListProd, IntList(..)) where
           --     ^^^^^^^^^^^
1
votes

The secret is here in the GHCi prompt:

*Main Lib>

For modules marked with a * (usually there is exactly one of these) all definitions are in scope, not just ones that are exported from the module. Modules without a * obey the usual rule: only exported names are visible in GHCi.