This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

(SB)CL: code creates endless loop while it is loaded...

6 posts · 6560 views

Hi,

(Gentoo Linux, SBCL 1.2.10)

I am at the very start of using LISP.

The code:
(let ((stream) (initialized 0))
  (defun readsched (schedname)
;; if called the first time, open stream
    (if (eql initialized 0 )
      (with-open-file (stream schedname)))
;; set the variable to 1, so stream will not be opened twice
    (setq initialized 1))
;; read a line from the stream, which is returned
    (read-line stream nil 'foo)) 
When I load a file containing this code, the prompt never comes back and
sbcl does a deep guru medition (or if you an Atari guy: Throws three bombs ;)
What did I wrong here?

Best regards,
mcc

Re: (SB)CL: code creates endless loop while it is loaded...

There are several parenthesis errors in your code (closing parens at wrong places). For writing Lisp code it's best to have a text editor that at least can highlight matching parens. Emacs is best for Lisp development, but has a learning curve. You can use any other editor you like, but it should have the capability to show you matching parens.

It's task of the reader to find the wrong parens while I'm writing the answer for the other thread. :D

- edgar

Re: (SB)CL: code creates endless loop while it is loaded...

There are other issues too. The with-open-file lexically encompasses code with an open stream (it opens the stream execute the code inside and closes the stream), but it appears you want to open the outside stream from let. For that you have functions: open, close and open-stream-p.

Moreover, i'd say it's generally bad idea to define a function (with defun) in a lexical closure (let).
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: (SB)CL: code creates endless loop while it is loaded...

In functional programming style you'd write functions working with the stream in a way that you'd pass the stream as an extra argument to them and then you'd just use with-open-file in code using these functions.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: (SB)CL: code creates endless loop while it is loaded...

Here's a "fixed" version of your function.
(let ((stream) (initialized 0))
  (defun readsched (schedname)
    ;; if called the first time, open stream
    (if (eql initialized 0 )
	(setf stream (open schedname)))
    ;; set the variable to 1, so stream will not be opened twice
    (setq initialized 1)
    ;; read a line from the stream, which is returned
    (read-line stream nil 'foo)))
The following links may be helpful.

http://cl-cookbook.sourceforge.net/files.html#line

http://www.gigamonkeys.com/book/ (See Chapter 14, Files and File I/O.)

Re: (SB)CL: code creates endless loop while it is loaded...

Hi all,

thank you so much for all your help!
I have now understood, that the problems I had mentioned in this
and my previous thread are the same.
I finally stiched something together, which (seems to) work.
Comments are heartly welcome!
(See my other thread...)

Best regards,
mcc