Ask coding questions

← Back to all posts
Variable not in scope
sara_n (2)

For every piece of code that I wrote, I get ''variable not in scope''. (for example :
length1 :: [a] -> Int
length1 l | l==[] =0
| otherwise = 1 + length1 tail l
)
some of the other solutions i tried are written by my teaher.

Commentshotnewtop
sara_n (2)

@MATTHEWBECHTEL I was wondering why it wouldnt work, but I solved the problem.

sara_n (2)

@MATTHEWBECHTEL thanks for wanting to help :)

MATTHEWBECHTEL (122)

Your welcome.
Upvote this comment if it helps @sara_n

MATTHEWBECHTEL (122)

Also Upvote all my comments that help @sara_n

ThatSmart (45)

Can you link your repl?

sara_n (2)

@ThatSmart Thanks so much for your help! what confused me the most is that similar code used to work a few days ago and also my very experienced teacher said it was fine. I'll try to figure out how to link it.

ThatSmart (45)

@sara_n It looks like duzina doesn't exist. Did you mean to say length? Also you're adding duzina to 1 in that code. You need to define a main function too.

sara_n (2)

@ThatSmart I was told i didnt need a main function, and all of the code I tried works without it. duzina means length so I dont see the problem in adding 1 to it. I wanted to write my own function for determining the length of an array.

ThatSmart (45)

@sara_n Haskell doesn't know that duzina means length though. Haskell doesn't know any function called duzina. It's in English, not Serbian/Bosnian. It should work if you replace duzina with length1.

ThatSmart (45)

@sara_n You're tyring to add 1 to the function, not the value.

sara_n (2)

@ThatSmart it doesn't matter what i call my function as long as i write my code well

ThatSmart (45)

@sara_n I know, but duzina isn't defined. Your function is called length1, or it was when I looked at it. You can definitely name your functions in another language, you just can't expect haskell to translate for you.

sara_n (2)

@ThatSmart what should i do then? the function returns an integer and i can add 1 to that integer and get the integer i need

sara_n (2)

@ThatSmart i tried the same with length1 and it still didnt work :( .

ThatSmart (45)

@sara_n Yes, you can. You just need to wrap length1 (tail l) or whatever it is now in parentheses.

ThatSmart (45)

@ThatSmart Oh if the function is called duzina then use duzina. I was looking at the wrong code.

ThatSmart (45)

@sara_n Replace | otherwise = 1 + length1 (tail l) with | otherwise = 1 + (length1 (tail l))

sara_n (2)

@ThatSmart it returns the same thing

sara_n (2)

@ThatSmart it returned ''variable not in scope''. But I wrote some easy code now just to see if it would run and after saying ''not in scope'' a few times it did run.

sara_n (2)

@ThatSmart It started working..?! I didnt change anything about the code I just clicked the run button first and did exactly like in the screenshot I sent. Thats so weird. Thanks for the help.

ThatSmart (45)

@sara_n No problem. Please mark one of my comments as the answer if they helped.

ThatSmart (45)

For one, you don't need that trailing parenthesis. Second, you need to add some parentheses because function calling has high precedence. Third, you need to add a type constraint for A if you are going to check if it's empty in this way. Also, I'd implement this with a foldl.

ThatSmart (45)

@ThatSmart Let me know if you need more help.

a5rocks (535)

Which language is this?

sara_n (2)

@a5rocks no, everything is indented I just didnt take care about that on here when i was writing the question. Thanks for the fast reply, though!

a5rocks (535)

@sara_n oof sorry, just don't know haskell oof