23

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]
wadie
  • 496
  • 1
  • 10
  • 24
Marcio
  • 331
  • 1
  • 3
  • 12
  • 6
    "Not in scope" means you are trying to use a name which is not defined in the place in which you are trying to use it. In this case, it happens because you left out a comma after `[1..x]`, and so your definition of `a` within the list comprehension doesn't work as it should. Change it to `[a | a <- [1..x], mod a x == 0]` – duplode Feb 08 '17 at 02:40
  • 1
    It help if you include whole GHCi error message in the question. – wizzup Feb 08 '17 at 05:23

2 Answers2

43

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> 
wizzup
  • 2,361
  • 20
  • 34
1

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.

mLstudent33
  • 1,033
  • 3
  • 14
  • 32