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

Discussion of Common Lisp
Post Reply
mcc
Posts: 18
Joined: Fri Mar 27, 2015 10:47 pm

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

Post by mcc » Sun Mar 29, 2015 1:49 am

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

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

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

Post by edgar-rft » Sun Mar 29, 2015 6:07 am

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

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

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

Post by Goheeca » Sun Mar 29, 2015 6:22 am

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.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

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

Post by Goheeca » Sun Mar 29, 2015 6:29 am

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.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

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

Post by nuntius » Sun Mar 29, 2015 8:16 am

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.)

mcc
Posts: 18
Joined: Fri Mar 27, 2015 10:47 pm

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

Post by mcc » Sun Mar 29, 2015 10:53 am

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

Post Reply