3

I'm trying to figure out how to read a line from a file with guile scheme.

When I ask it to "read port" or "read-char port", it successfully reads.

guile -c '(let ((port (open-input-file "foo.txt"))) (display (read port)) (newline) (close-port port))'

But, when I ask it to read-line, it fails.

guile -c '(let ((port (open-input-file "foo.txt"))) (display (read-line port)) (newline) (close-port port))'

Does anyone know what I am doing wrong? I am currently in the directory where foo.txt is located.

apaderno
  • 28,547
  • 16
  • 75
  • 90
user6189164
  • 667
  • 3
  • 7

1 Answers1

7

Your code fails with the message ERROR: Unbound variable: read-line, meaning that there is no definition for readline.

The function read-line has to be loaded using the form (use-modules (ice-9 rdelim)) before you can use it. (https://www.gnu.org/software/guile/manual/html_node/Input-and-Output.html)

This will then work:

guile -c '(use-modules (ice-9 rdelim)) (let ((port (open-input-file "foo.txt"))) 
(display (read-line port)) (newline) (close-port port))'
Terje D.
  • 6,250
  • 1
  • 22
  • 30