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

Re: newbie question, scheme idioms, functional approach to general
Patrick wrote:
> If I have a function f and I want
> to construct a list which contains the
> results of repeated calls to f with
> the additional constraint that the argument
> supplied to each next call to f is based
> on the result of the previous call, what
> is the Scheme way to solve this problem?
>
> The list should eventually look something
> like this:
>
> < f(x), f(f(x)), f(f(f(x))), ... >
>
> Or in Scheme,
>
> ( (f x) (f (f x)) (f (f (f x))) ... )
>
> Procedurally, I would use a temporary
> variable to track the most recent result
> of the series of nested calls to f.
>
> while (not_done())
>   {
>     x = f(x)
>     add_to_the_end_of_the_list(TheList, x)
>   }
>
> Very simple.
>
> What is the Scheme way to build the same
> kind of list from a function, f?
>

You do not say what is not_done() supposed to test? Number of elements
created or value of x? If I assume that it is is checking x, then
something like this would do:

(define (list-maker f done?)
(define (make x)
(if (done? x)
'()
(let ((x (f x)))
(cons x (make x)))))
make)

Here, you bind f and tester predicate.
The resulting function takes x and returns the required list.

Test run would look like this:

> (define make-test (list-maker (lambda (n) (- n 1)) zero?))
> (make-test 10)
(9 8 7 6 5 4 3 2 1 0)

-- Hrvoje

Report this thread to moderator Post Follow-up to this message
Old Post
Hrvoje Blazevic
04-18-05 01:58 PM


Re: newbie question, scheme idioms, functional approach to general
Hrvoje Blazevic wrote:
> Patrick wrote:
> 
>
> You do not say what is not_done() supposed to test? Number of elements
> created or value of x? If I assume that it is is checking x, then
> something like this would do:
>
> (define (list-maker f done?)
>   (define (make x)
>     (if (done? x)
>     '()
>     (let ((x (f x)))
>       (cons x (make x)))))
>     make)
>
> Here, you bind f and tester predicate.
> The resulting function takes x and returns the required list.
>
> Test run would look like this:
> 
> (9 8 7 6 5 4 3 2 1 0)
>
> -- Hrvoje


Thanks to everyone for these useful ideas.
They're very helpful.

--
Replace Roman numerals with digits to reply by email

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick
04-19-05 09:01 PM


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 07:19 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.