[plt-scheme] FrTime and sequential evaluation
Hi Jaime,
Combining explicit side effects with dataflow evaluation is tricky
(and typically not necessary). I would recommend trying to rewrite
this program without using set-cell!. If you want a mechanism for
generating events from the REPL, an easier way is to create
event-receivers, which you can then trigger with send-event. E.g.,
here's a program that does something similar to what I think you
intended:
(define start-e (event-receiver))
(define stop-e (event-receiver))
(define reset-e (event-receiver))
(define (start)
(send-event start-e (void)))
(define (stop)
(send-event stop-e (void)))
(define (reset initial)
(send-event reset-e initial))
(define running?
(hold (merge-e (map-e (lambda (_) #t) start-e)
(map-e (lambda (_) #f) stop-e))
#f))
(define tick (changes seconds))
(define (make-timer op)
(accum-b
(merge-e (map-e (lambda (e)
(if (second e) op identity))
(snapshot-e tick running?))
(map-e (lambda (v)
(lambda (_) v))
reset-e))
0))
At the REPL, try
> (make-timer -)
> (reset 10)
> (start)
> (stop)
etc.
Note that it won't stop automatically upon reaching a particular value.
Best,
Greg
On Sun, Jul 6, 2008 at 4:52 PM, Jaime Vargas <jev at mac.com> wrote:
> I have been playing with FrTime and I like it. However, I have a question.
> How do I enforce sequential evaluation?
> In the following code the procedurer count-down doesn't behave as
> expected.
>
> ;;; Definitions window
> (define elapsed (new-cell 0))
>
> (define (tick)
> (- seconds
> (value-now seconds)))
>
> (define (start-timer direction)
> (set-cell! elapsed (direction (value-now elapsed)
> (tick))))
>
> (define (stop-timer)
> (set-cell! elapsed (value-now elapsed)))
>
> (define (get-timer)
> elapsed)
>
> (define reset-timer
> (case-lambda
> [(v) (set-cell! elapsed v)]
> [() (set-cell! elapsed 0)]))
>
> (define (count-down v)
> (reset-timer v)
> (start-timer -))
>
> (define (trigger-alarm)
> (when (= 0 elapsed)
> (stop-timer)
> "wake-up"))
>
> ;; Interactions window
>> (get-timer)
> 0
>> (count-down 10)
>
> This starts counting down from 0, when I expected to count down from 10.
> Thanks,
> Jaime
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>