Message from discussion
Nested mapcar* application and possibly some variation of Y combinator
Path: g2news2.google.com!postnews.google.com!k15g2000yqd.googlegroups.com!not-for-mail
From: Swami Tota Ram Shankar <tota_...@india.com>
Newsgroups: gnu.emacs.help,comp.lang.lisp,comp.emacs,gnu.emacs,comp.lang.scheme
Subject: Re: Nested mapcar* application and possibly some variation of Y combinator
Date: Wed, 24 Aug 2011 19:56:14 -0700 (PDT)
Organization: http://groups.google.com
Lines: 64
Message-ID: <f4ea8d94-70a0-4edf-8fd6-429ae7361390@k15g2000yqd.googlegroups.com>
References: <2caa0c6e-9f98-446b-b5d5-3d09535efc13@f1g2000pre.googlegroups.com> <m3aab2emkc.fsf@barry_fishman.acm.org>
NNTP-Posting-Host: 75.28.103.167
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1314241061 29688 127.0.0.1 (25 Aug 2011 02:57:41 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 25 Aug 2011 02:57:41 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: k15g2000yqd.googlegroups.com; posting-host=75.28.103.167; posting-account=dlefMQoAAABzowG6c0cULB8igkwPchCd
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2)
Gecko/20100115 Firefox/3.6,gzip(gfe)
On Aug 21, 9:50=A0am, Barry Fishman <barry_fish...@acm.org> wrote:
> 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] ) =A0 =A0 ---> =A0 (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]] ) =A0 =A0 --->
> > ((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. =A0How 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)
> =A0 (mapcar* #'(lambda (x)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(if (sequencep x)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(map-r fun x)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(funcall fun x)))
> =A0 =A0 =A0 =A0 =A0 =A0seq))
>
> > 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 !
>
> > (
> > =A0(lambda (f n) (funcall f f n) )
> > =A0(lambda (f n) (if (zerop n) 1
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(* n (funcall f f (1- n))))
> > =A0 =A0)
> > =A05)
>
> 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). =A0And do
> this in a dynamically scoped lisp like e-lisp.
Your problem description is close to what I want.
However, no one has replied yet with any thing useful.
> I will leave that for somebody posting from a emacs group.
> --
> Barry Fishman