1 / 4
Feb 2005

(define (do_it n)
  (define (print_it n)
    (display n)
    (newline))
  (cond ((not(= n 42))
    (print_it n)
    (do_it (read)))))
(do_it (read))
  • created

    Feb '05
  • last reply

    Aug '12
  • 3

    replies

  • 1.3k

    views

  • 4

    users

2 years later

A perhaps more scheme-like solution:

(do ((i (read) (read)))
    ((eq? i 42) '())
  (begin
   (display i)
   (newline)))
10 days later

(((lambda (m)
    ((lambda (f)
       (m (lambda (a)
            ((f f) a))))
     (lambda (f)
       (m (lambda (a)
            ((f f) a))))))
  (lambda (r)
    (lambda (n)
      (cond ((not (eq? n 42))
             (display n)
             (newline)
             (r (read)))))))
 (read))
5 years later

Needless to say, if you want this to be fast, you need to use Stalin (qobi) rather than Guile (be sure your last expression returns 0 to avoid NZE). What may or may not be obvious to all is that using read is just barely too slow to get a time of 0.00, even with Stalin. You need to use read-char instead. I wrote mine using the trivial translation from DFA states to Scheme functions, and it worked just fine in Stalin. Under Guile, it was slower than the fastest Guile submissions—I'm not sure why.