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
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/
Note that I want to return (2 q), not 2. Or did I misunderstand your
reply?
-Carl
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. :-)
<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>
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))
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
Thats even better. That's what I like about Scheme, you can always
spot places to make it better :)