Page 1 of 1
(SB)CL: code creates endless loop while it is loaded...
Posted: Sun Mar 29, 2015 1:49 am
by mcc
Hi,
(Gentoo Linux, SBCL 1.2.10)
I am at the very start of using LISP.
The code:
Code: Select all
(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...
Posted: Sun Mar 29, 2015 6:07 am
by edgar-rft
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.
- edgar
Re: (SB)CL: code creates endless loop while it is loaded...
Posted: Sun Mar 29, 2015 6:22 am
by Goheeca
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).
Re: (SB)CL: code creates endless loop while it is loaded...
Posted: Sun Mar 29, 2015 6:29 am
by Goheeca
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.
Re: (SB)CL: code creates endless loop while it is loaded...
Posted: Sun Mar 29, 2015 8:16 am
by nuntius
Here's a "fixed" version of your function.
Code: Select all
(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...
Posted: Sun Mar 29, 2015 10:53 am
by mcc
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