Error haskell: not in scope. What does that mean?

Marcio picture Marcio · Feb 8, 2017 · Viewed 40.3k times · Source

I started with Haskell today and all the functions I perform on ghci display this message. I just want to know why this is happening. I know there are a lot of questions about this, but this is a simple case and I need to understand this error in the beginning

function3 :: Int -> [Int]
function3 x = [a | a <- [1..x] mod a x == 0]

Answer

wizzup picture wizzup · Feb 8, 2017

Did error happen when you type the function type in GHCi?

$ ghci
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Prelude> function3 :: Int -> [Int]

<interactive>:1:1: error:
    Variable not in scope: function3 :: Int -> [Int]
Prelude> 

If it is the case, you have to use multiple line input

Prelude> :{
Prelude| function3 :: Int -> [Int]
Prelude| function3 x = [a | a <- [1..x], mod a x == 0]
Prelude| :}

And noted , before mod

Alternatively, for better workflow, you can save your code to a file and load in GHCi using :load

$ cat tmp/functions.hs 
function3 :: Int -> [Int]
function3 x = [a | a <- [1..x], mod a x == 0]

$ ghci
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Prelude> :l tmp/functions.hs 
[1 of 1] Compiling Main             ( tmp/functions.hs, interpreted )
Ok, modules loaded: Main.
*Main> :t function3 
function3 :: Int -> [Int]
*Main>