Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - variable not in scope error - beginner

I'm new to haskell and cannot work out what is wrong with my code. I keep getting a variable out of scope error.

Here is my code that i load into GHCi using :load

validLength :: String -> Bool
validLength xs | length xs == 26 = True
               | otherwise = False

I then type validLength aa which should return false but i'm getting an error.

*Main> validLength aa

<interactive>:1:13: error: Variable not in scope: aa :: String
like image 444
Alisha White Avatar asked Oct 14 '19 14:10

Alisha White


1 Answers

Identifiers are not strings. (This is by no means special to Haskell, it's the same in most other languages.) So, when you give aa as the argument, GHC interprets it as the name of some variable. But, well, there is no variable with that name, at least not in scope, thus the error.

If you want to pass actually the string consisting of two a characters, then you should use a string literal. A string literal is simply a string in double quotes (again, that's the same way it is in many other programming languages).

*Main> validLength "aa"
False
like image 190
leftaroundabout Avatar answered Oct 17 '22 10:10

leftaroundabout