Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can't extract the digit-char?

72 views
Skip to first unread message

Jinsong Zhao

unread,
Apr 8, 2014, 10:27:28 PM4/8/14
to
Hi there,

Why the following code can't extract the digit-char?

(with-input-from-string (is "abc def 777 a b c")
(loop
:for c := (read-char is nil 'the-end)
:while (digit-char-p c)
:collect c))

What I expect result is (#\7 #\7 #\7), however I just get NIL.

Cannot understand why?

Regards,
Jinsong

Raymond Wiker

unread,
Apr 9, 2014, 12:20:22 AM4/9/14
to
Because you confuse :while with :when:

(with-input-from-string (is "abc def 777 a b c")
(loop
:for c := (read-char is nil 'the-end)
:while (not (eq c 'the-end))
:when (digit-char-p c)
:collect c))

There is also no need to specify the end marker for read-char (the
default nil makes for simpler and more readable code).

Personally, I wouldn't even use with-input-from-string and read-char in
this case; a simple (loop for c across ...) would be better.

Kaz Kylheku

unread,
Apr 9, 2014, 12:57:14 AM4/9/14
to
On 2014-04-09, Jinsong Zhao <jsz...@yeah.net> wrote:
> Hi there,
>
> Why the following code can't extract the digit-char?
>
> (with-input-from-string (is "abc def 777 a b c")
> (loop
> :for c := (read-char is nil 'the-end)
> :while (digit-char-p c)
> :collect c))

Let me whack you over the head with something.

Don't worry, it's very light-weight, and padded with soft parentheses:

(remove-if-not #'digit-char-p "abc def 777 a b c")

Jinsong Zhao

unread,
Apr 9, 2014, 4:36:59 AM4/9/14
to
Thanks a lot for explaining that. I am always confused by the condition
to terminate a loop. There are so many ways to terminate a loop.

Regards,
Jinsong

Jinsong Zhao

unread,
Apr 9, 2014, 4:38:29 AM4/9/14
to
Thank you very much for such simple solution. Like Perl, there's more
than one way to do it.

Regards,
Jinsong

WJ

unread,
Jun 21, 2014, 2:37:41 PM6/21/14
to
elisp:

: (replace-regexp-in-string (rx (not digit)) "" "abc def 777 a b c")
"777"

WJ

unread,
Mar 25, 2015, 1:29:30 AM3/25/15
to
Gauche Scheme:

(use gauche.sequence)

(filter #[\d] "abc def 777 a b c")
===>
(#\7 #\7 #\7)

(filter-to <string> #[\d] "abc def 777 a b c")
===>
"777"


WJ

unread,
Apr 25, 2016, 2:58:17 AM4/25/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
OCaml:

#load "str.cma";;

Str.global_replace (Str.regexp "[^0-9]") "" "abc 0 def 67 7 a 7 b c";;
===>
"06777"

--
[I]n Norway ... a straight white man [who] was gang raped by Somali immigrants
... is opposing ... deportation [of his rapist]. renseradioarchives.com/dduke/
This is war, and our greatest enemy is the enemy within: the submissive,
apologetic, guilt-ridden, self-hating drone.
www.kolumbus.fi/aquilon/londonspeech12.htm
0 new messages