Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "variable not in scope" error in GHCI?

Tags:

haskell

ghci

I have programmed a really basic recursive function, but when I try to use it Haskell gives me an error.

This is the code:

import Data.Char
import Test.QuickCheck
potencia :: Integer -> Integer -> Integer
potencia x 0 = 1
potencia x n = x*(potencia x (n-1))

And this is the error:

<interactive>:27:1-8: error:
    Variable not in scope: potencia :: Integer -> Integer -> t

If I delete the import of libreries it doesn´t give me the error anymore but I need them for later. I`m using the latest version of the Haskell platform.

like image 371
Miguel Vázquez Caraballo Avatar asked Mar 07 '23 08:03

Miguel Vázquez Caraballo


1 Answers

I see you are defining your function in interactive shell. Most of Haskell's REPLs read and eval instructions line-by-line, so when you type potencia :: Integer -> Integer -> Integer it is interpreted right at this moment, so compiler complains that potencia is lacking implementation. You should either:

  • Define it in external file and load it using :l (recommended)
  • Type :set +m and use let statement to define variable with respect to indentation
  • Surround your definition with :{ and :}
  • Put whole definition and declaration in one line separating each part with ;
like image 140
radrow Avatar answered Mar 16 '23 06:03

radrow