|
From: Dmitry I. <dfi...@gm...> - 2016-01-25 18:24:10
|
2016-01-25 20:50 GMT+03:00 Jeff Cunningham <je...@jk...>: > On 01/25/2016 06:47 AM, Stas Boukarev wrote: > > On Mon, Jan 25, 2016 at 5:37 PM, Jeff Cunningham > > <je...@jk...> wrote: > >> > >> On 01/24/2016 10:47 PM, Krzysztof Wicher wrote: > >>> Hi guys, > >>> > >>> I literally just started with common lisp and I believe I might've > >>> encountered a bug in sbcl. > >>> > >>> Simple example: > >>> (loop for c across (read-line (make-string-input-stream "aaa > >>> aaa")) do (format t "~a~%" (char-code c))) > >>> > >>> output when using CRLF line endings: > >>> 97 > >>> 97 > >>> 97 > >>> 13 > >>> > >>> output when using linux line endings: > >>> 97 > >>> 97 > >>> 97 > >>> > >>> > >>> this is causing simple programs as this (solution to: > >>> http://www.spoj.com/problems/TEST/ ): > >>> (loop > >>> (let > >>> ((line (read-line))) > >>> (if (equal line "42") (return)) > >>> (format t "~a~%" line))) > >>> > >>> not pass locally on Windows command prompt while passing on the server > >>> (this is how I found this bug) > >>> > >>> I'm running SBCL 1.3.1 > >>> with: > >>> sbcl --script test.lisp > >>> > >>> Cheers, > >>> Krzysztof > >> > >> The link problem doesn't say anything about reading from a text list > >> which just adds complexity. This will do what they asked: > >> > >> (loop for c in (list 1 2 88 42 99) until (= c 42) > >> do (print c)) > >> > >> Most of the time CR/LF's are a non-issue - an indication you're > >> approaching the problem wrong. For example, read eats whitespace - > >> including CR - just fine. You don't need to use readline for this > problem: > >> > >> (let ((is (make-string-input-stream "1 > >> 2 > >> 88 > >> 42 > >> 99"))) > >> (do ((v (read is nil nil) (read is nil nil))) > >> ((or (null v) (= v 42))) > >> (print v))) > >> > >> Or if you want it in a loop construct: > >> > >> (let ((is (make-string-input-stream "1 > >> 2 > >> 88 > >> 42 > >> 99"))) > >> (loop (let ((v (read is nil nil))) > >> (when (or (null v) (= v 42)) (return)) > >> (print v)))) > >> > >> CL-USER> > >> ; No value; compiling (LET (#) ...) > >> 1 > >> 2 > >> 88 > > Using READ for reading stuff that is not lisp source code is a really > > bad idea. Never do it. > > Why? > Because "read parses the printed representation of an object" (CLHS), while read-line just "reads from input-stream a line of text that is terminated by a newline or end of file". read-line is a low-level function that just reads from the stream without any parsing and just returns string rather then Lisp datum. |