Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

deepmap?
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

Report this thread to moderator Post Follow-up to this message
Old Post
carl
03-31-08 11:14 AM


Re: deepmap?
carl <clumma@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/

Report this thread to moderator Post Follow-up to this message
Old Post
Nils M Holm
03-31-08 11:14 AM


Re: deepmap?
On Mar 31, 12:14 am, Nils M Holm <news2...@t3x.org> wrote:
> carl <clu...@gmail.com> wrote: 
> 
> 
>
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
carl
03-31-08 11:15 AM


Re: deepmap?
carl <clumma@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. :-)

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

Report this thread to moderator Post Follow-up to this message
Old Post
Nils M Holm
03-31-08 11:16 AM


Re: deepmap?
On Mar 31, 8:49 am, carl <clu...@gmail.com> wrote:
> 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

<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>

Report this thread to moderator Post Follow-up to this message
Old Post
leppie
03-31-08 01:56 PM


Re: deepmap?
On Mar 31, 8:49 am, carl <clu...@gmail.com> wrote:
> 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

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))

Report this thread to moderator Post Follow-up to this message
Old Post
leppie
03-31-08 01:56 PM


Re: deepmap?
On Mar 31, 4:55 am, leppie <xacc....@gmail.com> wrote:
> 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))

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

Report this thread to moderator Post Follow-up to this message
Old Post
carl
04-01-08 03:19 AM


Re: deepmap?
On Mar 31, 8:39 pm, carl <clu...@gmail.com> wrote:
> On Mar 31, 4:55 am, leppie <xacc....@gmail.com> wrote:
>
>
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> 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 :)

Report this thread to moderator Post Follow-up to this message
Old Post
leppie
04-01-08 09:54 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Scheme archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:26 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.