chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] State machine using call/cc


From: Ed Watkeys
Subject: Re: [Chicken-users] State machine using call/cc
Date: Wed, 8 Dec 2004 10:42:00 -0500


On Dec 8, 2004, at 9:17 AM, Joel Reymont wrote:

Hi Ed!

Please tell me if I don't have a clue, but I think your goal should be
to abandon the idea of an explicit state machine and simply write your
code with procedure calls that wait for user input. That "simply
write..." part is where continuations work their magic.

I have the main application loop in C++. That is where I poll for network events. I supposed I could somehow write the server portion of my project
such that I poll for events from Scheme, maybe.

I'm sorry; I forgot about the main loop being in C++.

I also have the client portion of the project where the main loop is the SDL GUI loop I think that makes it harder to poll for events from Scheme.

I thought it would be much simpler to have Scheme be the event dispatcher
(application logic) and to leave the event gathering outside of Scheme.

Am I missing something?

I think Felix's suggestion, returning the next procedure to execute or placing it in a hash table -- might be simpler. You could use a closure and lambda to save your state. This is sort of like using currying (see Haskell). Someone else might have discussed this but here's an example:

(define (add-two-numbers)
  (letrec ((step1
            (lambda (n1)
              (if (or (< n1 1) (> n1 11))
                  (begin
                    (display "Sorry, 1 to 11 please.\n")
                    step1)
                  (begin
                    (display "Okay, continue.\n")
                    (letrec ((step2
                              (lambda (n2)
                                (cond
                                 ((= n2 69)
                                  (display "That number disgusts me!\n")
                                  step2)
                                 ((= n1 n2)
                                  (display "Let's start over, shall we?\n")
                                  step1)
                                 (else
                                  (display "Thank you, come again.\n")
                                  (+ n1 n2))))))
                      step2))))))
    step1))

#;> (define f (add-two-numbers))
#;> f
#<procedure>
#;> (define g (f 13))
Sorry, 1 to 11 please.
#;> (define h (g 11))
Okay, continue.
#;> (define i (h 69))
That number disgusts me!
#;> (define j (i 11))
Let's start over, shall we?
#;> (define k (j 3))
Okay, continue.
#;> (define l (k 4))
Thank you, come again.
#;> l
7

The only problem I can see is the ever-increasing level of indentation, but there are simple ways of dealing with that too.

Regards,
Ed

    Thanks, Joel

P.S. When I replied the list was not included. Was that your intention?

I accidentally replied to you and not the list. My mistake.

Ed

--
Watkeys Product Design * http://watkeys.com/
address@hidden * +1 215 694 4201





reply via email to

[Prev in Thread] Current Thread [Next in Thread]