Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

ghci

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]
like image 756
Marcio Avatar asked Feb 08 '17 02:02

Marcio


2 Answers

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> 
like image 111
wizzup Avatar answered Oct 06 '22 01:10

wizzup


For me it was trying to :reload my .hs file in ghci after opening a new session but doing :load full_file_name.hs solved the issue.

like image 44
mLstudent33 Avatar answered Oct 06 '22 02:10

mLstudent33