Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme: Cond "not equal"

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

like image 478
Frank Avatar asked Mar 28 '13 15:03

Frank


People also ask

How do you say not equal in scheme?

Boolean operations: and, or, not Since Scheme doesn't have a numeric "not equals" operator (like the != operator in C/Java/Python), we have to combine not and = in order to evaluate "not equals".

What does equal do in scheme?

equal? and = are checking for value equivalence, but = is only applicable to numbers. If you're using Racket, check here for more information. Otherwise, check the documentation of your scheme implementation.

How do you say not equal in a lisp?

/= (not Equal to) (AutoLISP) If only one argument is supplied, /= returns T. Note that the behavior of /= does not quite conform to other LISP dialects. The standard behavior is to return T if no two arguments in the list have the same value.

How do you check if a number is even in scheme?

If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd. You can check for this using num % 2 == 1 .


2 Answers

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))))
 ...)
like image 94
Chris Jester-Young Avatar answered Sep 28 '22 04:09

Chris Jester-Young


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>))
like image 40
robbyphillips Avatar answered Sep 28 '22 04:09

robbyphillips