5

I'd like to do this in scheme:

if ((car l) != (car (cdr (order l))) do something

in particular I wrote this:

((eq? (car l) (car (cdr (order l))) ) 
 (cons (count (car (order l)) (order l)) 
       (count_inorder_occurrences (cdr (order l))))) 

but it compares (car l) with (car (cdr (order l)) for equality. I want instead to do something only if eq? is false. How can I do that in my example?

Thanks

sds
  • 58,617
  • 29
  • 161
  • 278
Frank
  • 2,083
  • 8
  • 34
  • 52

3 Answers3

11

You can use not for that.

(cond
 ((not (eq? (car l) (cadr (order l))))
  (cons (count (car (order l)) (order l))
        (count-inorder-occurrences (cdr (order l))))
 ...)
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
1

You can use not to negate the value of a predicate.

e.g. in an if statement: (if (not (eq? A B)) <EVAL-IF-NOT-EQ> <EVAL-IF-EQ>)

or in a cond you can do:

(cond ((not (eq? A B))
       <EVAL-IF-NOT-EQ>)
      .
      .
      .
      (else <DEFAULT-VALUE>))
robbyphillips
  • 961
  • 5
  • 14
1

You don't really need the cond or if if you don't have a list of else cases. when might be what you are looking for. It's basically just the true case of an if.

(when (not (eq? (car l) (cadr (order l))))
   (cons 
      (count (car (order l)) (order l)) 
      (count-inorder-occurrences (cdr (order l)))
   )
)
edshaw
  • 11
  • 1