Re: Newbie lisp problem: string to list of strings separated by space



Yossarian wrote:

Hi,

I am trying to write a very simple function that accepts a string as
input, and that returns a list of strings as output, having a seperate
list item for every substring without a space.

Example:
input = "something very odd"
output = ("something" "very" "odd")

I tried to do this with the following function, but I can't understand
why it is not working.

(defun parse(a)
(setq pos (or (position #\space a) -1) )
(cond ((= pos -1) (list a))
((> pos -1) (append
(parse (subseq a 0 pos))
(parse (subseq a (+ pos 1) (length a)))))))

Any comment is welcome!

Regards,

Wouter

Or maybe like that:

(defun split-by-one-space (string)

"Returns a list of substrings of string
divided by ONE space each.
Note: Two consecutive spaces will be seen as if there were an empty string
between them."

(loop for i = 0 then (1+ j)
as j = (position #\Space string :start i)
collect (subseq string i j)
while j))

From cl-cookbook...

Cheers

--
*It is easy to lie with statistics, but it's a lot easier to lie without
them.
.



Relevant Pages

  • Re: OO versus RDB
    ... numbers in the Platonic realm! ... Otherwise it is a lie. ... claiming that a String class should really be named StringModel. ... Maybe your definition makes sense in platonic philosophy, ...
    (comp.object)
  • Cognitive Dissonant Oxymoron: "Dictatorship of Relativi
    ... > connotations in any given culture. ... >> string together words in order to deliberately misrepresent a situation ... That would be a lie, but it's not that the words are lies, ... It's like blaming science ...
    (sci.med.nutrition)
  • Re: OO versus RDB
    ... IMO this distinction is absolute and unambiguous. ... Any instance of string models some thing of the problem space. ... Now instead of String substitute SalariedEmployee, ... Isn't it a lie? ...
    (comp.object)
  • Re: NETFLIX has gone down the tubes
    ... Keep lying, wussuy, lie. ... He has you on his very *string*, with his inexorable power to make you respond every time he makes a major goofup in public!... ...
    (alt.video.dvd)
  • Re: Newbie lisp problem: string to list of strings separated by space
    ... (defun string-split (split-string string) ... "Returns a list containing items in 'string' split from ... for pos = ... if pos collect (subseq string n pos) ...
    (comp.lang.lisp)