0

I know Scheme and other Lisp dialects include logical operators such as 'and' and 'or', but, since I am currently learning Scheme, I am trying to program my own logical operators. So far my attempts at 'or' have been successful (at least so far as my testing has shown me). My logical or operator is the following:

(define (logical-or a b)
   (if a true b))

I am trying to program a logical operator for 'and' that also returns a boolean, but I keep getting stuck. I have tried any number of combinations, but I will just list the following one, which doesn't return a boolean:

(define (logical-and a b)
(if a b false))

Any hints or help welcome.

3
  • Welcome to Programming Puzzles & Code Golf! This site is for programming contests / challenges, not general programming questions. Therefore, I've migrated your question to Stack Overflow, where you can get help with your programming questions or problems. Commented Jul 19, 2014 at 5:15
  • 1
    Anything not false (actually #f) is true in Scheme Commented Jul 19, 2014 at 7:31
  • They aren't procedures. try (or #t (display "hello")) Commented Jul 19, 2014 at 13:16

2 Answers 2

2

Your logical-or does not always return booleans either:

> (logical-or #f 2)
2

As @leppie says, anything not false (#f) is true in Scheme; try experimenting a little with the build-in or function:

> (or 1 2)
1
> (or #f 2)
2
> (or #t 2)
#t
> (or 1 #f)
1
> (or #f #t)
#t

so the definition in Scheme would be:

(define (my-or a b)
  (if a a b))

Likewise, for and:

> (and 1 #t)
#t
> (and 1 #f)
#f
> (and 1 2)
2
> (and #f 2)
#f
> (and #t 2)
2

so the definition is

(define (my-and a b)
  (if a b a))

If you want to only return booleans, then you'd code

(define (logical-or a b)
  (cond
    (a    #t)
    (b    #t)
    (else #f)))

(define (logical-and a b)
  (if a
      (if b 
          #t 
          #f)
      #f))

This works for 2 values, but since the build-in and and or operators allow any number of parameters (even 0) and only evaluate their parameters if necessary the real definitions are a little more complicated.

Sign up to request clarification or add additional context in comments.

Comments

2

As Uselpa's answer and the leppie's comments mentioned, the operators and and or don't return #t but the value that was not #f that decided the outcome of the form. Thus

(and 'these 'are 'all 'true 'values)  ; ==> values
(or  'these 'are 'all 'true 'values)  ; ==> these

The logical operators and and or are short circuiting so they are not procedures. Imagine this procedure:

(define (first-true-value lst)
  (and (pair? lst)
       (or (car lst) 
           (first-true-value (cdr lst)))))

(first-true-value '())            ; ==> #f
(first-true-value '(#f #f))       ; ==> #f
(first-true-value '(#f #f hello)) ; ==> hello

If you replace and and or with your versions the procedure will never stop evaluating the recursion.

We know we can rewrite and with if. (and) is #t, (and a) is a and (and a b ...) is (if a (and b ...) #f). We could do this with th easiest Scheme macros, syntax-rules:

(define-syntax logical-and 
  (syntax-rules ()
    ((logical-and) #t)
    ((logical-and a) a)
    ((logical-and a b ...)
     (if a (logical-and b ...) #f)))) 

We can also do or the same way. (or) is #f and (or a b ..) is (if a a (or b ...)):

(define-syntax logical-or 
  (syntax-rules ()
    ((logical-or) #f)
    ((logical-or a b ...) ; NB: zero elements match "b ..."
     (if a a (logical-or b ...))))) 

There is a problem with this one since it uses a twice.. Try (logical-or (display "hello")). It will evaluate (display "hello") and thus display the text twice. To fix this we need to wrap the value in a let:

(define-syntax logical-or 
  (syntax-rules ()
    ((logical-or) #f)
    ((logical-or a b ...) 
     (let ((tmp a))
       (if tmp 
           tmp
           (logical-or b ...)))))) 

If you try the same it will only display "hello" once. Lets try writing my initial procedure with the new macros:

(define (first-true-value lst)
  (logical-and (pair? lst)
               (logical-or (car lst) 
                           (first-true-value (cdr lst)))))

;; and we test them:
(first-true-value '())            ; ==> #f
(first-true-value '(#f #f))       ; ==> #f
(first-true-value '(#f #f hello)) ; ==> hello

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.