Re: Newbie lisp problem: string to list of strings separated by space
- From: Pascal Bourguignon <usenet@xxxxxxxxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 11:44:42 +0200
Yossarian <yossarian@xxxxxxxxxxx> writes:
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!
Does this give you a hint?
[2]> (parse "something very odd")
*** - Lisp stack overflow. RESET
[3]> (trace parse)
;; Tracing function PARSE.
(PARSE)
[4]> (parse "something very odd")
1. Trace: (PARSE '"something very odd")
2. Trace: (PARSE '"something")
2. Trace: PARSE ==> ("something")
2. Trace: (PARSE '"something very odd")
3. Trace: (PARSE '"something")
3. Trace: PARSE ==> ("something")
3. Trace: (PARSE '"something very odd")
4. Trace: (PARSE '"something")
4. Trace: PARSE ==> ("something")
4. Trace: (PARSE '"something very odd")
5. Trace: (PARSE '"something")
5. Trace: PARSE ==> ("something")
5. Trace: (PARSE '"something very odd")
....
If not yet, try this:
[8]> (defun parse(a)
(setq pos (or (position #\space a) -1) )
(cond ((= pos -1) (list a))
((> pos -1) (append
(parse (subseq a 0 (print pos)))
(parse (subseq a (+ (print pos) 1) (length a)))))))
;; ^^^^^^
WARNING: DEFUN/DEFMACRO: redefining PARSE; it was traced!
PARSE
[9]> (trace parse)
;; Tracing function PARSE.
(PARSE)
[10]> (parse "something very odd")
1. Trace: (PARSE '"something very odd")
9
2. Trace: (PARSE '"something")
2. Trace: PARSE ==> ("something")
-1
2. Trace: (PARSE '"something very odd")
9
3. Trace: (PARSE '"something")
3. Trace: PARSE ==> ("something")
-1
3. Trace: (PARSE '"something very odd")
9
4. Trace: (PARSE '"something")
4. Trace: PARSE ==> ("something")
-1
4. Trace: (PARSE '"something very odd")
9
5. Trace: (PARSE '"something")
5. Trace: PARSE ==> ("something")
-1
5. Trace: (PARSE '"something very odd")
9
6. Trace: (PARSE '"something")
6. Trace: PARSE ==> ("something")
-1
6. Trace: (PARSE '"something very odd")
9
7. Trace: (PARSE '"something")
7. Trace: PARSE ==> ("something")
-1
7. Trace: (PARSE '"something very odd")
9
You are modifying a global variable pos, so its value changes between
the two recursive calls to parse.
Use a local variable! Use LET
--
__Pascal Bourguignon__ http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush
.
- Follow-Ups:
- References:
- Prev by Date: Re: packages, load paths and environment variables
- Next by Date: Re: (make-pathname :name "/root") - good/bad?
- Previous by thread: Newbie lisp problem: string to list of strings separated by space
- Next by thread: Re: Newbie lisp problem: string to list of strings separated by space
- Index(es):
Relevant Pages
|