Scheme: Cond "not equal"

48,539

Solution 1

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

Solution 2

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

Solution 3

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>))
Share:
48,539
Frank
Author by

Frank

Updated on October 19, 2020

Comments

  • Frank
    Frank over 3 years

    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

  • Frank
    Frank about 11 years
    thanks to you too, I can't use "if" if not it would be simpler as you wrote