4 ms·
Well, Common Lisp has such a construct built in: CL-USER 48 > (LOOP REPEAT 2 FOR number = (READ) IF (> number
by lispm 10y ago
Well, Common Lisp has such a construct built in:
CL-USER 48 > (LOOP REPEAT 2
FOR number = (READ)
IF (> number 0) DO
(princ '(the number is larger than 0))
(print 'true)
(terpri)
ELSE DO
(princ '(the number is not larger than 0))
(print 'false)
(terpri))
2
(THE NUMBER IS LARGER THAN 0)
TRUE
-2
(THE NUMBER IS NOT LARGER THAN 0)
FALSE
NIL
It is also not that difficult to write a multi-expression IF. Here is a sketch:
(defmacro if* (cond &rest conseq)
(let ((p cond)
(thenpos (position 'then conseq))
(elsepos (position 'else conseq)))
(cond ((and thenpos elsepos)
`(if ,p
(progn ,@(subseq conseq (1+ thenpos) elsepos))
(progn ,@(subseq conseq (1+ elsepos)))))
(thenpos `(when ,p ,@(subseq conseq (1+ thenpos))))
(elsepos `(unless ,p ,@(subseq conseq (1+ elsepos)))))))
Then we can write:
(if* (> pi 0)
then (print 'a) (print 'b)
else (print 'c) (print 'd))
and
(if* (> pi 0)
then (print 'a) (print 'b))
and
(if* (> pi 0)
else (print 'a) (print 'b))
- qwertyuiop924 10y agoIf it's part of LOOP, than it's not part of the language, now is it? Well, it is, but you can't do it in the general case. You can define a macro for it, though.