Ask coding questions

← Back to all posts
2
Variable not in scope

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
3

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.

1

@ThatSmart Let me know if you need more help.

2

Can you link your repl?

1

@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.

1

@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.

1

@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.

1

@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.

1

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

1

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

1

@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.

1

@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

1

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

3

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

1

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

3

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

1

@ThatSmart it returns the same thing

1
1

@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.

1

@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.

1

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

1

What is your question\? @sara_n

1

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

1

@MATTHEWBECHTEL thanks for wanting to help :)

1

Your welcome.
Upvote this comment if it helps @sara_n

1

Also Upvote all my comments that help @sara_n

1

Which language is this?

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!

1

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