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

Generalizing append_map

35 views
Skip to first unread message

Merciadri Luca

unread,
Apr 6, 2012, 8:32:38 AM4/6/12
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I've got

==
(define append_map
(lambda (f l)
(if (null? l) '() (append (f (car l)) (append_map f (cdr l))))))
==

which is equivalent to

==
(define append_map
(lambda (f l) (apply append (map f l))))
==

I now want to generalize this to an arbitrary number of arguments. I
know this can be done using (lambda v) at the place of (lambda (v))
(this is called `generalized lambda form'). How could I use this in
this context (e.g. one function f and an arbitrary number of lists)?

Thanks.
- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --

If all else fails, try the obvious.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk9+4mUACgkQM0LLzLt8MhyxxACeLPaA0D9BeK/a561C+fYEX3jq
Qa4Anj45JWhw0iIlCdvhfIYLeql1gchD
=BPyS
-----END PGP SIGNATURE-----

Jussi Piitulainen

unread,
Apr 6, 2012, 11:43:51 AM4/6/12
to
Merciadri Luca writes:

> Hi,
>
> I've got
>
> ==
> (define append_map
> (lambda (f l)
> (if (null? l) '() (append (f (car l)) (append_map f (cdr l))))))
> ==
>
> which is equivalent to
>
> ==
> (define append_map
> (lambda (f l) (apply append (map f l))))
> ==
>
> I now want to generalize this to an arbitrary number of arguments. I
> know this can be done using (lambda v) at the place of (lambda (v))
> (this is called `generalized lambda form'). How could I use this in
> this context (e.g. one function f and an arbitrary number of lists)?

With care. I suggest writing append-map-1 (what you have above), then
append-map-2 (two argument lists), then append-map-3 (three argument
lists), and by then you probably see the pattern which you can
generalize to append-map-n (any positive number of argument lists). It
worked for me.

At the moment I don't see how zero argument lists would also fit the
pattern or make sense. Mind stops working.

It's useful to know that (apply proc arg1 arg2 argses) is the same as
(apply proc (cons arg1 (cons arg2 argses))).

namekuseijin

unread,
Apr 11, 2012, 8:04:38 PM4/11/12
to
Em sexta-feira, 6 de abril de 2012 09h32min38s UTC-3, Merciadri Luca escreveu:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
>
> I've got
>
> ==
> (define append_map
> (lambda (f l)
> (if (null? l) '() (append (f (car l)) (append_map f (cdr l))))))
> ==
>
> which is equivalent to
>
> ==
> (define append_map
> (lambda (f l) (apply append (map f l))))
> ==
>
> I now want to generalize this to an arbitrary number of arguments. I
> know this can be done using (lambda v) at the place of (lambda (v))
> (this is called `generalized lambda form'). How could I use this in
> this context (e.g. one function f and an arbitrary number of lists)?

whatever you do, change that C identifier to lisp style... :)

Merciadri Luca

unread,
Apr 22, 2012, 2:01:49 PM4/22/12
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks for the help.
If it's too good to be true, then it probably is.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk+UR40ACgkQM0LLzLt8MhyGhQCeKQXF8El60VnvmxtiV0kL/Gsw
TV0An2mn7HWq3C23F7Y1ZoifGuHY0+0i
=KZ68
-----END PGP SIGNATURE-----
0 new messages