Reputation: 23
I am having trouble grasping the concept of how to correctly make a multiple condition if statement in scheme. An example would be something like
if(a>b && b<c){
action;
}
In most programming languages i am familiar with, but this is proving a daunting task in scheme.
(define (my-list-ref lst)
(cond (and((>(car lst)0) (<(cadr lst)0))
((+ n 1)(my-list-ref (cdr list)))
(and((<(car lst)0) (>(cadr lst)0)))
(zero? car lst)
(my-list-ref(cdr lst))
(null? lst)
(display n))))
With the first lines of the cond code i am trying to say that if the car of the list is greater than 0, and the cadr is less than 0, I want it to take a certain action, yet i am greeted with a bad syntax error for the and operator. I am deeply confused, would greatly appreciate an explanation.
Upvotes: 1
Views: 5446
Reputation: 31145
The syntax of cond
works like this:
(cond
[question1 expression1]
[question2 expression2]
[question3 expression3]
[else expression4])
Note that I am using square brackets. In Scheme square brackets and normal parenthesises mean the same. It's just easier to see the grouping when square brackets are used.
Your example becomes something like this:
(define (my-list-ref lst)
(cond
[(and (> (car lst) 0) (< (cadr lst) 0)) (my-list-ref (cdr list))]
[(and (< (car lst) 0) (> (cadr lst) 0)) some-action-here]
[(zero? car lst) (my-list-ref(cdr lst))]))
Note here that the syntax of and
works like this:
(and expression1 expression2)
So when you want check whether (> (car lst) 0)
and (< (cadr lst) 0)
at the same type, the cond-question becomes (and (> (car lst) 0) (< (cadr lst) 0))
.
Upvotes: 2