chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Termbox using


From: Jim Ursetto
Subject: Re: [Chicken-users] Termbox using
Date: Wed, 6 May 2015 22:50:29 -0500

Hi frad,

If I may make a suggestion. Lists are not random-access like arrays are. 
Similarly, checking a list's length traverses the whole list.

You should always iterate over a list by using car and cdr, not list-ref. So, 
to your "do" loop, you could add another loop variable over the list called L, 
iterate over it using cdr, and as the termination condition, test with null?. 
Then your list-ref becomes car. The way you have it, the algorithm is O(n^2). 
If you need random access you should use a vector, however that is not 
necessary for your test program.

Jim

> On May 2, 2015, at 07:34, address@hidden wrote:
> 
> Hi,
> 
> (Sorry for my English)
> 
> Newbie with Scheme, i try to use the termbox egg.
> It seems me a very nice and cool library and the documentation helps me a lot.
> I am learning Scheme and appreciate Chicken implementation.
> Here a little piece of code to print a simple text menu.
> But this code isn't in the functionnal way (very iterative).
> 
> ----
> (use termbox)
> (init)
> 
> (define running (make-parameter #t))
> (define choice 1)
> 
> (define (menu x y lst)
>  (let loop ()
>    (if (> choice (length lst)) (set! choice 1))
>    (if (< choice 1) (set! choice (length lst)))
>    (do ((i 0 (+ i 1)))
>        ((= i (length lst)))
>        (if (= choice (+ i 1))
>            (bprintf x (+ y i) (style black) (style white) (list-ref lst i))
>            (bprintf x (+ y i) (style white) (style black) (list-ref lst i))))
>    (present)
>    (poll (lambda (mod key ch)
>              (cond
>                ((eq? key key-arrow-down) (set! choice (+ choice 1)))
>                ((eq? key key-arrow-up) (set! choice (- choice 1)))
>                    ((eq? key key-enter) (running #f))))
>          (lambda (w h) (loop)))
>    (when (running) (loop))))
> 
> ;; an example
> (menu 5 8 '("Choice 1" "Choice 2" "Choice 3" "Other"))
> 
> (shutdown)
> 
> (printf "Your choice: ~a\n" choice)
> ----
> 
> Maybe, someone could give me some advices or corrections to make it better.
> 
> Regards,
>  frad
> 
> http://blog.indexi.net
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Chicken-users mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/chicken-users



reply via email to

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