Reputation: 815
I write a script with using random and with-open-file, it works well in slime by emacs. But it cannot work when I use sbcl compile it to a execute file.
My purpose is using this code to choice the random video to open.;update at 1/1/2016
Code:;update at 1/1/2016
(setf *random-state* (make-random-state t))
(defun choice-file-to-open (files) ;file is a list content all pathspecs which I want to open
(let ((filePath (nth (random (length files)) files)))
(open-by-system filePath) ;use shell command "open" to open file
(with-open-file (file "./logs" :direction :output
:if-exists :append
:external-format '(:utf-8 :replacement #\?))
(format file "~S~%" (namestring filePath))) ;write filename in log file to record the open history
))
open-by-system
is a function to open file.
My purpose is pick random file in the folder. But it always choice the same file to open when I use it. Only for the singer execute file compiled by sbcl, the slime during emacs work well.
Then I add the log file to record the filename every time I open. But there is no log file, as same as problem before, this problem only issues in executed file, and code works well in slime. with-open-file won't work in singer execute file compiled, but slime work well.
I found the answer (Random) in Common Lisp Not So Random? and it cannot solve the random problem.
What wrong with me? There are many differences between slime and sbcl?
Upvotes: 2
Views: 344
Reputation: 815
I fix them by myself. There are two problems in my old codes. Firstly, random not work well. Secondly, the with-open-file
don't work.
The codes worked below:
(defun choice-file-to-open (files) ;file is a list content all pathspecs which I want to open
(let ((filePath (nth (random (length files) (make-random-state t)) files)))
(open-by-system filePath) ;use shell command "open" to open file
(with-open-file (file "./logs"
:direction :output
:if-does-not-exist :create
:if-exists :append
:external-format '(:utf-8 :replacement #\?))
(format file "~S~%" (namestring filePath))) ;write filename in log file to record the open history
))
(make-random-state t)
should be added at the end of random
. Then fix the random problem.
:if-does-not-exist :create
should be add because the log file don't existed. Then fix the log file can't create problem.
Upvotes: 1