CKneeland
CKneeland

Reputation: 119

Scheme - Beginner Syntax Issues with greater than, less than, and an and condition

I'm just starting to write in scheme in DrRacket. However, like we all probably know, those small changes in syntax always mess us up!

I believe my error is in the and conditional. If someone could take a look and tell me what's wrong that would be great!

; in-range?: int int list of numbers --> boolean
(define in-range?
  (lambda (a b s)
    (cond [(null? s) #t] ;List is empty
          [(null? a) #f]
          [(null? b) #f]
          [((and >= (car s)) ((a) <= (car s)) (b)) (in-range? (a) (b) (cdr s))]
          [else #f]
     )))

Upvotes: 1

Views: 2275

Answers (1)

Sylwester
Sylwester

Reputation: 48745

Imagine this form:

(test a b)

You can see it is an application because of the parentheses when test is not a syntax operand. Then test is evaluated and the expected outcome is a procedure that can be called with the evaluated arguments a and b.

You have this as the only epression in a cond term:

((and >= (car s)) ((a) <= (car s)) (b)) (in-range? (a) (b) (cdr s))

This is an application. (and >= (car s)) ((a) <= (car s)) (b)) is not a syntax operand. Then it is evaluated and the expected outcome is a procedure that takes at least one argument, the evaluation of (in-range? (a) (b) (cdr s)).

Since the expression is and which is a syntax operand we know it will either be #f or #t and you should have gotten an error like Application: not a procedure

Parentheses around something in Scheme is like parentheses after something in algol languages like C# and JavaScript. Imagine this expression:

((a.car >=)(), (<= a() s.car)())(in-range(a(), b(), s.cdr))

Lots of syntax errors there in JavaScript too :-o

Upvotes: 3

Related Questions