[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Convert string of words to list of strings

William James

5/21/2015 8:46:00 AM

David Bakhash wrote:

> here's the function. It's been around the block a few times. It does the
> job, and I use it all the time.
>
> (defun split (string &optional max (ws #(#\space #\tab #\newline #\return)))
> "Split `string' along whitespace as defined by the sequence `ws'.
> The whitespace is elided from the result. The whole string will be
> split, unless `max' is a non-negative integer, in which case the
> string will be split into `max' tokens at most, the last one
> containing the whole rest of the given `string', if any."
> (declare (type string string)
> (type (or null fixnum) max)
> (type sequence ws))
> (flet ((is-ws (char)
> (declare (type character char))
> (find char ws)))
> (loop
> for start = (position-if-not #'is-ws string)
> then (position-if-not #'is-ws string :start index)
> for index = (and start
> (unless (and max (= (1+ word-count) max))
> (position-if #'is-ws string :start start)))
> while start
> collect (subseq string start index)
> count 1 into word-count
> while index)))

Gauche Scheme:

The hard way:

(use srfi-13 :only (string-trim))
(use gauche.sequence :only (find-index))

(define (split text :optional (limit 0) (ws " \t"))
(define (is-ws ch) (string-scan ws ch))
(define (next-word)
(set! text (string-trim text is-ws))
(cond ((equal? "" text) (eof-object))
((= limit 1) (begin0 text (set! text "")))
(else
(dec! limit)
(let1 end (find-index is-ws text)
(begin0
(string-copy text 0 end)
(set! text (if end (string-copy text end) "")))))))
(generator-map identity next-word))

gosh> (split " foo bar a b c ")
("foo" "bar" "a" "b" "c")
gosh> (split " foo bar a b c " 3)
("foo" "bar" "a b c ")

The easy way:

(use srfi-13 :only (string-trim-both))

gosh> (string-split (string-trim-both " foo bar a b c ") #/\s+/)
("foo" "bar" "a" "b" "c")
gosh> (string-split (string-trim-both " foo bar a b c ") #/\s+/ 3)
("foo" "bar" "a" "b c")

--
However, we shall see that The Authoritarian Personality extends beyond the
attempt to pathologize cohesive gentile groups to pathologizing adaptive gentile
behavior in general. The principal intellectual difficulty is that behavior
that is critical to Judaism as a successful group evolutionary strategy is
conceptualized as pathological in gentiles. --- Kevin MacDonald; "The Frankfurt
School of Social Research and the Pathologization of Gentile Group Allegiances"
1 Answer

William James

12/1/2015 6:56:00 AM

0

WJ wrote:

> David Bakhash wrote:
>
> > here's the function. It's been around the block a few times. It does the
> > job, and I use it all the time.
> >
> > (defun split (string &optional max (ws #(#\space #\tab #\newline #\return)))
> > "Split `string' along whitespace as defined by the sequence `ws'.
> > The whitespace is elided from the result. The whole string will be
> > split, unless `max' is a non-negative integer, in which case the
> > string will be split into `max' tokens at most, the last one
> > containing the whole rest of the given `string', if any."
> > (declare (type string string)
> > (type (or null fixnum) max)
> > (type sequence ws))
> > (flet ((is-ws (char)
> > (declare (type character char))
> > (find char ws)))
> > (loop
> > for start = (position-if-not #'is-ws string)
> > then (position-if-not #'is-ws string :start index)
> > for index = (and start
> > (unless (and max (= (1+ word-count) max))
> > (position-if #'is-ws string :start start)))
> > while start
> > collect (subseq string start index)
> > count 1 into word-count
> > while index)))

Ocaml:

#load "str.cma";;

Str.split (Str.regexp " +") " foo bar a b c " ;;
===>
["foo"; "bar"; "a"; "b"; "c"]

Str.bounded_split (Str.regexp " +") " foo bar a b c " 2;;
===>
["foo"; "bar a b c "]

--
Viewed at its most abstract level, a fundamental agenda is thus to influence
the European-derived peoples of the United States to view concern about their
own demographic and cultural eclipse as irrational and as an indication of
psychopathology. --- Dr. Kevin MacDonald; "The Frankfurt School of Social
Research and the Pathologization of Gentile Group Allegiances"