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

deepmap?

0 views
Skip to first unread message

carl

unread,
Mar 31, 2008, 2:49:43 AM3/31/08
to
Hi,

I'm an amateur Schemer (Petite Chez in particular) making a first
post.

I want to do this:

(deepmap min (lambda (x) (car x)) '((3 p) (2 q))) => (2 q)

Like,

(define deepmap
(lambda (proc filter ls)
...))

It seems like something that might require continuations, but I don't
know how to use them.

Any tips would be most appreciated. Thanks,

-Carl

Nils M Holm

unread,
Mar 31, 2008, 3:14:21 AM3/31/08
to
carl <clu...@gmail.com> wrote:
> (deepmap min (lambda (x) (car x)) '((3 p) (2 q))) => (2 q)
>
> [...]

>
> It seems like something that might require continuations, but I don't
> know how to use them.

There is no need for continutations (although you can certainly
construct a clever solution that uses them). Just find the minimum
of the car and cdr parts of your list (by recursing) and then
return the smaller one of them. Finding the trivial case of the
recursion is left as an exercise. :-)

--
Nils M Holm <n m h @ t 3 x . o r g> -- http://t3x.org/nmh/

carl

unread,
Mar 31, 2008, 3:17:46 AM3/31/08
to

Note that I want to return (2 q), not 2. Or did I misunderstand your
reply?

-Carl

Nils M Holm

unread,
Mar 31, 2008, 4:38:28 AM3/31/08
to
carl <clu...@gmail.com> wrote:
> Note that I want to return (2 q), not 2. Or did I misunderstand your
> reply?

In your example I would consider '(2 q) the minimum of '(2 q) and '(3 p).
Of course, you would not use 'min' (because it does not know about the
data structure you are going to traverse) but '<' in combination with
your accessor function. Or is there any reason why you need to use 'min'?

At this point it would probably be best to start coding and return when
you get stuck. I do not want to spoil the fun entirely. :-)

leppie

unread,
Mar 31, 2008, 7:50:03 AM3/31/08
to

<pre style='color:black;border:1 solid windowtext;background-
color:white;font-family:Consolas,Bitstream Vera Sans Mono,Lucida
Console,Courier New;'><FONT color=DarkBlue>(</FONT><FONT
color=Blue>define</FONT> <FONT color=DarkBlue>(</FONT>deepmap p f
lst<FONT color=DarkBlue>)</FONT>
<FONT color=DarkBlue>(</FONT><FONT color=Teal>fold-left</FONT>
<FONT color=DarkBlue>(</FONT><FONT color=Blue>lambda</FONT> <FONT
color=DarkBlue>(</FONT>prev x<FONT color=DarkBlue>)</FONT>
<FONT color=DarkBlue>(</FONT><FONT color=Blue>if</FONT> prev
<FONT color=DarkBlue>(</FONT><FONT color=Blue>let</FONT> <FONT
color=DarkBlue>(</FONT><FONT color=DarkBlue>(</FONT>nm <FONT
color=DarkBlue>(</FONT>p <FONT color=DarkBlue>(</FONT>f prev<FONT
color=DarkBlue>)</FONT> <FONT color=DarkBlue>(</FONT>f x<FONT
color=DarkBlue>)</FONT><FONT color=DarkBlue>)</FONT><FONT
color=DarkBlue>)</FONT><FONT color=DarkBlue>)</FONT>
<FONT color=DarkBlue>(</FONT><FONT color=Blue>if</FONT>
<FONT color=DarkBlue>(</FONT><FONT color=Teal>eqv?</FONT> <FONT
color=DarkBlue>(</FONT>f prev<FONT color=DarkBlue>)</FONT> nm<FONT
color=DarkBlue>)</FONT>
prev
x<FONT color=DarkBlue>)</FONT><FONT color=DarkBlue>)</
FONT>
x<FONT color=DarkBlue>)</FONT><FONT color=DarkBlue>)</FONT>
<FONT color=Red>#f</FONT>
lst<FONT color=DarkBlue>)</FONT><FONT color=DarkBlue>)</FONT></pre>

leppie

unread,
Mar 31, 2008, 7:55:00 AM3/31/08
to
On Mar 31, 8:49 am, carl <clu...@gmail.com> wrote:

Sorry, didnt know this does not support HTML, here is the text
version.

(define (deepmap p f lst)
(fold-left
(lambda (prev x)
(if prev
(let ((nm (p (f prev) (f x))))
(if (eqv? (f prev) nm)
prev
x))
x))
#f
lst))

carl

unread,
Mar 31, 2008, 2:39:26 PM3/31/08
to

Cool, thanks! Using eqv was the insight I missed (duh).
Any reason why not to write it like this

(define deepmap
(lambda (proc filter ls)

(fold-left
(lambda (prev x)
(if (eqv?
(proc (filter prev) (filter x))
(filter prev))
prev
x))
(car ls)
(cdr ls))))

?

-Carl

leppie

unread,
Apr 1, 2008, 1:43:22 AM4/1/08
to

Thats even better. That's what I like about Scheme, you can always
spot places to make it better :)

0 new messages