[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

how to get the result?

Jinsong Zhao

5/8/2015 4:21:00 PM

Hi there,

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. What's the
problem? Thanks a lot.

Regards,
Jinsong
5 Answers

Pascal J. Bourguignon

5/8/2015 4:17:00 PM

0

Jinsong Zhao <jszhao@yeah.net> writes:

> Hi there,
>
> 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. What's the
> problem? Thanks a lot.

Check the syntax for DO*!

(do* (,@bindings)
(,stop-condition ,result)
,@body)

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Jinsong Zhao

5/8/2015 4:33:00 PM

0

On 2015/5/9 0:16, Pascal J. Bourguignon wrote:
> Jinsong Zhao <jszhao@yeah.net> writes:
>
>> Hi there,
>>
>> 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. What's the
>> problem? Thanks a lot.
>
> Check the syntax for DO*!
>
> (do* (,@bindings)
> (,stop-condition ,result)
> ,@body)
>

Sorry for the noise. I should be more careful...
Regards,
Jinsong

William James

5/8/2015 8:51:00 PM

0

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.

Gauche Scheme:

(use srfi-42 :only (string-ec))
(call-with-input-string "Hello, how are you today?"
(lambda (port)
(string-ec
(:while (: c port read-char) (not (char=? c #\space)))
c)))

===>
"Hello,"


Another way:

(car (string-split "Hello, how are you today?" " " 1))
===>
"Hello,"


Another way:

((#/\S+/ "Hello, how are you today?") 0)
===>
"Hello,"


Another way:

(string-scan "Hello, how are you today?" " " 'before)
===>
"Hello,"

--
An important part of the explanation is the role of mass media in Sweden. Not a
single TV-program, radio program, or big newspaper would give space to critics
of the multicultural project.

William James

5/29/2016 3:41:00 PM

0

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:

let first_word str =
String.sub str 0 (try (String.index str ' ')
with Not_found -> String.length str) ;;

# first_word "Hello, how are you today?" ;;
- : string = "Hello,"
# first_word "Hello,how" ;;
- : string = "Hello,how"

--
When there is nothing left on earth but individuals without any roots, sans
racial identity or pride, deprived of any religion, and existing only to be a
consumer, the Jews---who, however, will have kept their own traditions and
identity---will be recognized by all as "God's Chosen People." -- Herve Ryssen
katana17.wordpress.com/2014/10/17/herve-ryssen-interviewed-by-margaret-huffstickler/

Kaz Kylheku

5/29/2016 4:27:00 PM

0

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,"