Search code examples
schemeguile

read-line in let won't read the next line


  1 (use-modules (ice-9 rdelim))
  2 
  3 (define (show l) (display l))
  4 
  5 (define (read-two-lines)
  6         (let ((count  (read-line))
  7               (l      (read-line)))
  8              (show l))) ; or (show count)
  9              
 10 (read-two-lines)

The code above fails to read the second line into l. Instead it just reads the same value twice :-

scheme@(guile-user)> (load "test.scm")

line1
line2
line1

When line1 followed by line2 is given as input it shows line1 as output instead of line2 . What is happening here?


Solution

  • I don't have Guile installed so I cannot test this, but in Scheme let does not guarantee any order of evaluation. So my best guess is that count contains the second line, and l the first.

    Try using let* instead of let:

    (define (read-two-lines)
      (let* ((count  (read-line))
             (l      (read-line)))
        (show l)))
    

    For a more thorough explanation see here.