Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fif function from On Lisp

1 view
Skip to first unread message

weber

unread,
Mar 4, 2008, 7:31:19 AM3/4/08
to
Hi folks.
For some reason this function from On Lisp doesn't work:

(defun fif (a b &optional c)
'(lambda (x)
(if (funcall a x)
(funcall b x)
(unless (null c) (funcall c x)))))

I should be able to call it like this:

(mapcar (fif 'zerop '1+ 1-) '(0 1 2 3) => (1 0 1 2)

but i'm probably missing something that is differs from Common Lisp to
Elisp.

Thanks in advance,
weber

Johan Bockgård

unread,
Mar 4, 2008, 7:53:51 AM3/4/08
to
weber <hug...@gmail.com> writes:

The code above doesn't work in Common Lisp either, but anyway, Emacs
Lisp doesn't have lexical closures (yet).

(info "(elisp) Extent")

--
Johan Bockgård

weber

unread,
Mar 4, 2008, 8:59:59 AM3/4/08
to

Hm. Does that snippet really depends on lexical closures?
I just need that function to expand to this:

(mapcar (lambda (x) (if (funcall 'zerop x) (funcall '1+ x) (funcall
'1- x)))
'(0 1 2 3))

... maybe I can solve it with a macro then?

-weber

weber

unread,
Mar 4, 2008, 9:07:36 AM3/4/08
to

Ok, i can.
Tks anyway.

(defmacro fif (a b &optional c)
`(lambda (x)
(if (funcall ,a x)
(funcall ,b x)
(unless (null ,c) (funcall ,c x)))))

-weber

weber

unread,
Mar 4, 2008, 9:12:35 AM3/4/08
to

Just needed to post a correct version:

(defmacro fif (a b &optional c)
`(lambda (x)
(if (funcall ,a x)
(funcall ,b x)

(if (null ,c) x (funcall ,c x)))))

-weber

Mike Mattie

unread,
Mar 5, 2008, 7:34:33 PM3/5/08
to help-gn...@gnu.org

(require 'cl)

(defun plus (x)
(+ x 1))

(defun minus (x)
(- x 1))

(defun fif (a b &optional c)

(lexical-let
((a-fn a)
(b-fn b)
(c-fn c))

(lambda (x)
(if (funcall a-fn x)
(funcall b-fn x)
(if (functionp c-fn) (funcall c-fn x))))))

(mapcar (fif 'zerop 'plus 'minus) '(0 1 2 3)) ;; => (1 0 1 2)

This works here.

Cheers,
Mike Mattie

signature.asc
0 new messages