Why is Python popular, while Lisp and Scheme aren't?
Pascal Costanza
costanza at web.de
Wed Nov 13 08:33:09 EST 2002
Andreas Leitgeb wrote:
> I'd have rewritten it differently (to show the point I was
> trying to make by calling the scheme-snippet ugly):
> 2 if (cardList.size() == 0) return false;
> 3 else if (!cardList.get(0).isVisible()) return false;
> 4 else return ((slotId == 1) || (slotId > 5));
> Now this doesn't make a big difference here, but it makes
> a lot of a difference, once, things get longer and deeper nested.
I wanted to try the following, so I did. I don't if this would help in
practice, though... ;)
(defun chained-if (form &key (level 0))
(cond
((symbolp form) form)
(t (let ((rest (nthcdr 2 form)))
(cond
(rest
(case (first form)
('and `(if not ,(second form) then return false
else ,@(chained-if (cons 'and rest)
:level level)))
('or `(if ,(second form) then return true
else ,@(chained-if (cons 'or rest)
:level level)))))
((> level 0)
`(,(chained-if (second form) :level (1- level))))
(t
`(,(second form))))))))
(defun cprint (form)
(mapc (lambda (elm)
(when (eq elm 'else) (format t "~%"))
(format t "~A " elm))
form))
Now you can do the following.
> (cprint
(chained-if
'(and (not (null card-list))
(is-visible-p (first card-list))
(or (= slot-id 1)
(> slot-id 5)))))
if not (not (null card-list)) then return false
else if not (is-visible-p (first card-list)) then return false
else (or (= slot-id 1) (> slot-id 5))
> (cprint
(chained-if
'(and (not (null card-list))
(is-visible-p (first card-list))
(or (= slot-id 1)
(> slot-id 5)))
:level 1))
if not (not (null card-list)) then return false
else if not (is-visible-p (first card-list)) then return false
else (if (= slot-id 1) then return true else (> slot-id 5))
All the best,
Pascal
--
Pascal Costanza University of Bonn
mailto:costanza at web.de Institute of Computer Science III
http://www.pascalcostanza.de Römerstr. 164, D-53117 Bonn (Germany)
More information about the Python-list
mailing list