f



Returning Control from a "loop" prematurely >>>Newbie Question<<<

Hi.

Let's say I had a function g, which takes an integer
as an argument and will either return a positive integer
if it has some special property, otherwise it will
return 0.  Most of the time, for arbitrary inputs,
g is going to return 0.

Now, suppose I want to set up a loop, and call g
with successive arguments (g 1), (g 2), (g 3), and
so on...

This loop is going to terminate when the loop counter
gets to some sentinel value or ,perhaps sooner , if that
that special property is found.

In C the code might look something like

int f (int n)
   {
     int result = 0;

     f (int i = 0; i < n; ++i)
       {
         result = g(n);

         if (result != 0)
           i = n;
       }

     return result;
   }


What g does specifically isn't that important.  The
g I am working with is actually very complicated but
for the sake of having a concrete example, in this
case g will return the quotient of n divided by 11
only if 11 divides n evenly; g will return zero
otherwise.

In Scheme I have:

(define (g n)
     (if (= (modulo n 11) 0)
         (quotient n 11)
         0))

(define (f n)
     (define (f-iter i)
       (if (> i n)
           0
           (if (not (= (g i) 0))
               (f-iter (+ i 1))
               (g i))))
     (f-iter 1))

Obviously f isn't working how I want it to since this
function always seems to return 0.

I'm curious to see how people do this kind of thing
in Scheme since unlike C, it doesn't seem like you
can just let control "fall out of the loop", break,
or "force" a return from a function.

In a previous post of mine someone suggested that
I use the trace function to debug some code.  Trace
sounds like a great tool but as far as I can tell it's
not available to me in PLT Scheme.  My current language
setting is R5RS.   Perhaps trace, on that implementation
goes by another name?

Thanks in advance.
0
5/19/2005 2:28:58 PM
comp.lang.scheme 4781 articles. 0 followers. Post Follow

5 Replies
358 Views

Similar Articles

[PageSpeed] 41

Patrick <mathgeekXXXXII@hotmail.com> wrote:
>      f (int i = 0; i < n; ++i)
>        {
>          result = g(n);
> 
>          if (result != 0)
>            i = n;
>        }

You can (roughly) rewrite this fragment as

for (i=0; i<n && g(i) != 0; i++) ;

(Assuming that you meant
- "for (int" instead or "f (int"
- "result = g(i)" instead of "result = g(n)"
)

This way you avoid the ugly premature exit and
get a version that should be easier to translate
to Scheme.

> I'm curious to see how people do this kind of thing
> in Scheme since unlike C, it doesn't seem like you
> can just let control "fall out of the loop", break,
> or "force" a return from a function.

Of course you can. Look up the R5RS procedure
"call-with-current-continuation" in your manual.

Nils

-- 
Nils M Holm <nmh@despammed.com>         http://www.holm-und-jeschag.de/nils/
Symbolic Computing - an Introduction to Pure LISP: http://www.t3x.org/scipl/
0
nmh1 (59)
5/19/2005 3:55:55 PM
Patrick <mathgeekXXXXII@hotmail.com> schrieb:
> I'm curious to see how people do this kind of thing
> in Scheme since unlike C, it doesn't seem like you
> can just let control "fall out of the loop", break,
> or "force" a return from a function.

Look up call-with-current-continuation (often abbreviated
call/cc). You can use continuations to do anything you can
do with labels/gotos/return/break/continue; the basic form
is:

(call/cc (lambda (return)
            ...
	    (return someval) ; when you need to escape
	    ...))
0
adrian-news (121)
5/19/2005 4:00:38 PM
Patrick wrote:
> Let's say I had a function g, which takes an integer
> as an argument and will either return a positive integer
> if it has some special property, otherwise it will
> return 0.  Most of the time, for arbitrary inputs,
> g is going to return 0.
> 
> Now, suppose I want to set up a loop, and call g
> with successive arguments (g 1), (g 2), (g 3), and
> so on...
> 
> This loop is going to terminate when the loop counter
> gets to some sentinel value or ,perhaps sooner , if that
> that special property is found.
....
> What g does specifically isn't that important.  The
> g I am working with is actually very complicated but
> for the sake of having a concrete example, in this
> case g will return the quotient of n divided by 11
> only if 11 divides n evenly; g will return zero
> otherwise.
> 
> In Scheme I have:
> 
> (define (g n)
>     (if (= (modulo n 11) 0)
>         (quotient n 11)
>         0))
> 
> (define (f n)
>     (define (f-iter i)
>       (if (> i n)
>           0
>           (if (not (= (g i) 0))
>               (f-iter (+ i 1))
>               (g i))))
>     (f-iter 1))
> 
> Obviously f isn't working how I want it to since this
> function always seems to return 0.

I think you have your conditional in f-iter reversed.  When (= (g i) 0), then 
i does not divide n evenly.  So in that case, you want to recur.  Also, you 
call g twice on the same argument, so you're needlessly duplicating 
computation (that is, if g is side-effect free), and since you said g is 
complicated, this may be a significant waste.

Here is one way to write it.  In g, I return false, rather than zero to 
indicate that the number does not have the property in question.  Likewise I 
return false from f when a number having the property of g cannot be found.

;; g will return the quotient of n divided by 11
;; only if 11 divides n evenly; g will return _FALSE_
;; otherwise.

(define (g n)
   (and (zero? (modulo n 11)) (quotient n 11)))

;; Call (g i) for 1 <= i <= n, and return (g i) whenever (g i) is not false. 
;; Return _FALSE_ if no such value is found.

(define (f n)
   (let loop ((i 1))
      (and (<= i n)
           (or (g i)
               (loop (add1 i))))))

Alternatively, using SRFI 42:

(define (f n)
   (first-ec #f (: i 1 n) (g i)))

HTH,
David
0
dvanhorn1 (74)
5/19/2005 5:14:57 PM
Patrick wrote:
> I'm curious to see how people do this kind of thing
> in Scheme since unlike C, it doesn't seem like you
> can just let control "fall out of the loop", break,
> or "force" a return from a function.

See
   <http://schemecookbook.org/view/Cookbook/IdiomPrematureReturn>.

--=20
Jens Axel S=F8gaard
0
usenet8944 (1130)
5/19/2005 6:46:08 PM
Jens Axel S�gaard wrote:
> Patrick wrote:
> 
>> I'm curious to see how people do this kind of thing
>> in Scheme since unlike C, it doesn't seem like you
>> can just let control "fall out of the loop", break,
>> or "force" a return from a function.
> 
> 
> See
>   <http://schemecookbook.org/view/Cookbook/IdiomPrematureReturn>.
> 

Wow, that seems to answer my question _exactly_.

Thanks to all respondents.
0
5/20/2005 2:44:31 PM
Reply:

Similar Artilces:

<<<<<<<<<<<<<The Real A.I PROBLEMS">>>>>>>>>>>>>>>>>>>>>>>>>>>
The unvierse is like a ballon with black holes and white holes picture two on each side left two right on upside down the speed and the amout of matter consumed is a direct relations ship two the size of the ballon as the ballon get bigger the gravitaion forces of the black holes increase two self balance the universe it's self fixing but as time go's on and matter moves at diffrent speed's in diffrent dementions and at diffrent rates the demnetions become unstable becasue of misplaced matter ( magnetic plasma hint hint" oppset of black matter. ( sun spots ) why is the...

"<<previous<< 2 3 4 5 >>next>>" scheme
hi all, I am creating a web project with php/mysql. one of qeury result is too high. it returs several thousand rows. But I can't show them in one page. So I wanna create more than one pages then I shall use "<<previous<< 2 3 4 5 >>next>>" scheme. but i don't have any idea. If user clicks 3rd page. I shall show the 3rd page. How should I do this ? should I again query the whole or a portion ? what is the function related to this. Thank in advance Also sprach hSiplu: > I am creating a web project with php/mysql. > one of qeury result is too high. it returs several thousand rows. > But I can't show them in one page. > So I wanna create more than one pages > then I shall use "<<previous<< 2 3 4 5 >>next>>" scheme. > but i don't have any idea. > If user clicks 3rd page. I shall show the 3rd page. > How should I do this ? > should I again query the whole or a portion ? what is the function > related to this. You could use MySQL's LIMIT clause. if I use select * from t1 limit 10; it shows first 10 returned rows. It wont work. it would work if an range could be used oh I have found it ! limit [offset,] row_count that solves everything :) ...

"new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>] ")"
According to http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9 the following production holds <ClassInstanceCreationExpression> ::= "new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>] ")" What would be an example for a class instance creation expression with type arguments? Here's a reminder about those: <TypeArguments> ::= "<" <TypeArgument> {"," <TypeArgument>} ">" What I would understand would be: <ClassInstanceCre...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%>
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** Hi! > You too can learn to be funny in just 7 days flat: ....spamming newsgroups, though, just ISN'T funny. Even this newsgroup... Path: attbi_s22!attbi_s21!attbi_slave12!attbi_master11!wn14feed!worldnet.att.net!1 99.218.7.141!news.glorb.com!newsfeed-east.nntpserver.com!nntpserver.com!stat ler.nntpserver.com!hydra.nntpserver.com!not-for-mail From:...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #6
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #4
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #2
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #2
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #2
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #2
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #3
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #2
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #2
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

<%><%><%><%>HOW TO BE FUNNY!<%><%><%><%> #2
You too can learn to be funny in just 7 days flat: http://www.ExpertHumor.com/HowToBeFunny.htm *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** ...

Web resources about - Returning Control from a "loop" prematurely >>>Newbie Question<<< - comp.lang.scheme

Resources last updated: 3/29/2016 8:54:03 PM