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.

You are viewing a single comment. View All
ThatSmart (42)

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 (42)

@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 (42)

@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 (42)

@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 (42)

@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 (42)

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

ThatSmart (42)

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

ThatSmart (42)

@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 (42)

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