Kaz Kylheku
5/29/2016 4:27:00 PM
On 2016-05-29, WJ <w_a_x_man@yahoo.com> wrote:
> WJ wrote:
>
>> Jinsong Zhao wrote:
>>
>> > I run the following code in REPL, but the result is not returned.
>> >
>> > (with-input-from-string (strm "Hello, how are you today?")
>> > (do* ((c (read-char strm) (read-char strm))
>> > (s (string c) (concatenate 'string s (string c))))
>> > ((char= c #\space))
>> > s))
>> >
>> > The expected result is "Hello,", however, I just got NIL.
>
> OCaml:
TXR, reading from string stream like the Lisp:
$ txr -i
1> (with-in-string-stream (s "Hello, world") (read-until-match #/\s/ s))
"Hello,"
> let first_word str =
> String.sub str 0 (try (String.index str ' ')
> with Not_found -> String.length str) ;;
Retarded nonsense in a noisy garbage language.
> # first_word "Hello, how are you today?" ;;
> - : string = "Hello,"
> # first_word "Hello,how" ;;
> - : string = "Hello,how"
You fucking imbecile. The question is about using a string stream, not how to
find the first space using a crude method. But anyway:
Common Lisp:
[1]> (flet ((first-word (s) (subseq s 0 (position #\space s))))
(first-word "Hello, world"))
"Hello,"
[2]> (flet ((first-word (s) (subseq s 0 (position #\space s))))
(first-word "Hello,world"))
"Hello,world"
TXR Lisp:
1> (flet ((first-word (s) [s 0..(posql #\space s)]))
(first-word "Hello, world"))
"Hello,"
2> (flet ((first-word (s) [s 0..(posql #\space s)]))
(first-word "Hello,world"))
"Hello,world"
See how there is no try/with Not_found -> String.length noise here?
We could use LOOP and it would still look better than OCaml.
(loop for char across "Hello, world"
until (char= char #\space)
collecting char into word
finally (return (coerce word 'string))) --> "Hello,"