(defun hungry-and (x &rest y)
(format t "arguments: ~a~&" x) ; but the function will stop checking them
; once it sees first argument that evaluates to nil
(when x (when y (apply 'hungry-and y))))
(hungry-and :foo :bar 42 t nil "string") ; arguments are evaluated here(defun my-and (&rest args)
(cond ((not args)
;; There are no arguments, default to true
t )
(t (and (car args) (apply #'my-and (cdr args)))) ))yesimthetaxman wrote:I am trying to write a simple boolean AND function that uses eager/strict evaluation. That is, I want both arguments to the AND function to be evaluated. I am able to write a function to simulate this using IF statements, but it is long, I am wondering if anyone has any ideas how to write a shorter, more elegant version that evaluates the two arguements and then AND's them...
(defun my-and (x y) (and x y))
wvxvw wrote:Let's see if it helps, I'm not exactly sure where do you want to evaluate arguments, but I assumed that you didn't want to continue checking the arguments after you are sure about the result:
- Code: Select all
(defun hungry-and (x &rest y)
(format t "arguments: ~a~&" x) ; but the function will stop checking them
; once it sees first argument that evaluates to nil
(when x (when y (apply 'hungry-and y))))
(hungry-and :foo :bar 42 t nil "string") ; arguments are evaluated here
(defun hungry-and (x &rest y)
(format t "arguments: ~a~&" x) ; but the function will stop checking them
; once it sees first argument that evaluates to nil
(when x (every 'or y)))(defun hungry-and (&rest args) (every #'identity args))wvxvw wrote:Oh, sorry, it wasn't just a bit wrong, it was a whole lot wrongWell...
- Code: Select all
(defun hungry-and (x &rest y)
(format t "arguments: ~a~&" x) ; but the function will stop checking them
; once it sees first argument that evaluates to nil
(when x (every 'or y)))
A fixed version. Although, I must note that smithzv would also be wrong when called with empty list for example.
(define-compiler-macro hungry-and (&rest args)
(let ((syms (loop repeat (length args) collect (gensym))))
`(let ,(mapcar #'list syms args)
(and ,@syms))))Users browsing this forum: Google [Bot] and 5 guests