25

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]
2
  • 7
    "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] Commented Feb 8, 2017 at 2:40
  • 1
    It help if you include whole GHCi error message in the question. Commented Feb 8, 2017 at 5:23

2 Answers 2

48

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

In what case would function3 :: Int -> [Int] not throw an error in ghci ?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.