# Is this OR procedure correct? [closed]

(define or-l (lambda x
(if (null? x)
#f
(if (car x) #t
(apply or-l (cdr x))))))

(map (lambda (x) (apply or-l x)) '((#t #t) (#f #t) (#t #f) (#f #f)))
'(#t #t #t #f)


It will be used in a function that evaluates Boolean expressions.

• Welcome to Code Review! What is your actual question? If you're asking whether it runs, please check the help center first. At the least we're going to need more information before we can say anything meaningful about it. What is it supposed to do and what will call this function? – Mast Apr 22 at 12:31
• @Mast I've simplified the question. Please tell me if anything is still unclear and otherwise remove the -1. – X10D Apr 23 at 11:28
• The question is answered now, so I'm not sure the question should be edited now. But for the next time, please keep the previous link (to the help center) and our FAQ on asking questions in mind. – Mast Apr 23 at 13:10
• how will the -1 impact this question? – X10D Apr 24 at 13:09
• It won't. Your reputation still rose a net 9 points by asking this question and it isn't scoring low enough to be in danger of anything drastic. It will only impact your account if you keep asking questions where the same thing happens, eventually you'll be temporarily question-banned. Read that help center and our FAQ on asking questions before asking the next one, don't worry, and feel free to ask for help. If you put in the effort, I'm sure you'll be ok. – Mast Apr 24 at 15:02

## or-l Is a Poor Name

The OP procedure or-l is correct in that it yields correct results for its inputs. This or-l short-circuits only in the sense that recursive calls through the provided arguments cease when a true value is encountered; yet, this is not the behavior of or, which is a special form. The or-l procedure always evaluates all of its arguments, where the or special form evaluates its arguments sequentially until a true value is encountered.

Consider:

scratch.rkt> (or #t (/ 1 0))
#t
scratch.rkt> (or-l #t (/ 1 0))
; /: division by zero


Here or does not evaluate (/ 1 0) because it is a special form which only evaluates forms as needed. But, or-l is a procedure which always evaluates all of its arguments; (/ 1 0) is evaluated before the procedure body is entered, and this evaluation results in an error.

The name or-l should be reconsidered. The name suggests that this procedure applies or to a list, which it does not; it applies or to an unspecified number of arguments. It would be better, perhaps, to call it or-proc to emphasize that it behaves similarly to or, but that it is a procedure and thus evaluates its arguments.

## A Better Way

OP has written a somewhat lengthy recursive function which is then applied via map and an anonymous function to a list of boolean lists to achieve OP goal:

(define or-proc
(lambda x
(if (null? x)
#f
(if (car x)
#t
(apply or-proc (cdr x))))))

(map (lambda (x) (apply or-proc x)) '((#t #t) (#f #t) (#t #f) (#f #f)))
'(#t #t #t #f)


A simpler and cleaner approach would be to take advantage of built-in Racket features such as ormap. Note that (ormap identity '(#t #f)) is equivalent to (or (identity #t) (identity #f)), i.e. (or #t #f).

With this in mind, here is a better or-l procedure which actually takes a list argument:

(define (or-l xs)
(ormap identity xs))


This procedure can be used more cleanly with the list-of-lists input provided by OP:

scratch.rkt> (map or-l '((#t #t) (#f #t) (#t #f) (#f #f)))
'(#t #t #t #f)