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

Re: I don't understand Lisp

117 views
Skip to first unread message

WJ

unread,
Oct 28, 2012, 8:16:41 PM10/28/12
to
Erik Naggum wrote:

> (defun delimited-substrings (string &rest delimiters)
> "Return as multiple values the substrings delimited by each delimiter."
> (loop
> with substrings = ()
> for delimiter in delimiters
> for start = 0 then (+ end length) ;after last delimiter ends
> for end = (search delimiter string :start2 start)
> for length = (length delimiter)
> do (push (subseq string start end) substrings)
> until (null end) ;the string was exhausted
> finally
> (when end ;the delimiters were exhausted
> (push (subseq string (+ end length)) substrings))
> (return (values-list (nreverse substrings)))))

Truly incredible and truly pitiful.

Let's test it:

* (delimited-substrings "foo bar" " ")

"foo"
" bar"



Racket:

(string-split " eeny meeny miney mo ")

--> '("eeny" "meeny" "miney" "mo")

(string-split ",DARPA CL:still.stinking;after,all these years."
#rx"[,;:. ]+")

--> '("DARPA" "CL" "still" "stinking" "after" "all" "these" "years")

(string-split ",DARPA CL:still.stinking;after,all these years."
#rx"[,;:. ]+" #:trim? #f)

--> '("" "DARPA" "CL" "still" "stinking" "after" "all" "these" "years" "")

(string-split ",DARPA CL:still.stinking;after,all these years."
#rx"(,all these |[,;:. ]+)")

--> '("DARPA" "CL" "still" "stinking" "after" "years")

WJ

unread,
Feb 22, 2015, 12:44:05 AM2/22/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
WJ wrote:

> Erik Naggum wrote:
>
> > (defun delimited-substrings (string &rest delimiters)
> > "Return as multiple values the substrings delimited by each delimiter."
> > (loop
> > with substrings = ()
> > for delimiter in delimiters
> > for start = 0 then (+ end length) ;after last delimiter ends
> > for end = (search delimiter string :start2 start)
> > for length = (length delimiter)
> > do (push (subseq string start end) substrings)
> > until (null end) ;the string was exhausted
> > finally
> > (when end ;the delimiters were exhausted
> > (push (subseq string (+ end length)) substrings))
> > (return (values-list (nreverse substrings)))))
>
> Truly incredible and truly pitiful.
>
> Let's test it:
>
> * (delimited-substrings "foo bar" " ")
>
> "foo"
> " bar"

Gauche Scheme:

(string-split "CLXQISZJCOBOLQZLIKE" #/[XQZJ]+/)
===>
("CL" "IS" "COBOL" "LIKE")

(string-split "CL,IS,COBOL,LIKE" #\,)
===>
("CL" "IS" "COBOL" "LIKE")

(string-split "CLFOOISFOOCOBOLFOOLIKE" "FOO")
===>
("CL" "IS" "COBOL" "LIKE")

(string-split "CL-IS COBOL-LIKE" (cut char<? <> #\A))
===>
("CL" "IS" "COBOL" "LIKE")

WJ

unread,
Jul 27, 2016, 8:38:55 AM7/27/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
OCaml:

#load "str.cma";;

Str.(split (regexp "[ \t]+") " eeny meeny miney mo ")

===>
["eeny"; "meeny"; "miney"; "mo"]

--
The goal is to meet the EU challenge of interracial marriage. It's not a
choice. It's an obligation. If voluntarism does not work for the republic,
then the state will move in with more coercive measures.
--- the Jew Nicolas Sarkozy (www.youtube.com/watch?v=K0hD7IffTJs)
0 new messages