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

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

Re: Nested mapcar* application and possibly some variation of Y combinat


From: Barry Fishman
Subject: Re: Nested mapcar* application and possibly some variation of Y combinator
Date: Wed, 28 Mar 2012 19:21:30 -0000
User-agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.50 (gnu/linux)

On 2011-08-21 04:19:20 EDT, Swami Tota Ram Shankar wrote:
> Dear elispWizards,

I'm far from an elisp wizard, but I can give you something that works.

> Consider the following command to halve every element in a vector or a
> list
>
> (mapcar* '(lambda(x) (* 0.5 x)) '[1 2 3 4 5 6 7] )     --->   (0.5 1.0
> 1.5 2.0 2.5 3.0 3.5)
>
> Now, I intend to vary it so that it operated like this on a singly
> nested list
>
> (mapcar* '(lambda(x) (* 0.5 x)) '[[1 2 3] [4 5 6 7]] )     --->
> ((0.5 1.0 1.5) (2.0 2.5 3.0 3.5))
>
> It would be nice if this can be accomplished without opening the
> nested list or vector and nesting it again.

I'm not sure what you mean.  How else would you do it?

> I could not put the mapcar* inside the lambda ... maybe made some
> simple mistake. I dont have a strong enough intuition to say if this
> is possible or not.

If you want a function that traverses sequences recursively you might
do something like:

(defun map-r (fun seq)
  (mapcar* #'(lambda (x)
               (if (sequencep x)
                   (map-r fun x)
                 (funcall fun x)))
           seq))

> However, I am inspired by this approach for factorial which I picked
> up from somewhere a while ago. Its said that it is a Y combinator
> implmentation and it works, and I kinda understand it, but if someone
> has a crisp and constructive methodical derivation from problem
> statement, you're very welcome !
>
> (
>  (lambda (f n) (funcall f f n) )
>  (lambda (f n) (if (zerop n) 1
>                  (* n (funcall f f (1- n))))
>    )
>  5)

I assume you want a create a function for use in a mapcar* which knows
how to call itself when faced with a nested sequence, so you don't have
to regenerate a lambda function at each nest level (as I did).  And do
this in a dynamically scoped lisp like e-lisp.

I will leave that for somebody posting from a emacs group.
-- 
Barry Fishman


reply via email to

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