help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fif function from On Lisp


From: Mike Mattie
Subject: Re: fif function from On Lisp
Date: Wed, 5 Mar 2008 16:34:33 -0800

On Tue, 4 Mar 2008 04:31:19 -0800 (PST)
weber <hugows@gmail.com> wrote:

> 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

(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

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]