Re: Newbie lisp problem: string to list of strings separated by space
- From: Yossarian <yossarian@xxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 14:31:39 +0200
Pascal Bourguignon wrote:
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
Indeed, the idea was to use a local variable ...
Now, if I replace "setq" with "let", then I receive the following error.
Are there extra requirements for using "let"? What is wrong now?
(defun parse(a)
(let 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)))))))
;;; Warning: Function not defined: PARSE
;;; An error occurred in function #< COMPILED-FUNCTION: #xD0F828 >:
;;; Error: The variable A is unbound
.
- Follow-Ups:
- Re: Newbie lisp problem: string to list of strings separated by space
- From: Pascal Bourguignon
- Re: Newbie lisp problem: string to list of strings separated by space
- From: Pascal Bourguignon
- Re: Newbie lisp problem: string to list of strings separated by space
- References:
- Newbie lisp problem: string to list of strings separated by space
- From: Yossarian
- Re: Newbie lisp problem: string to list of strings separated by space
- From: Pascal Bourguignon
- Newbie lisp problem: string to list of strings separated by space
- Prev by Date: Re: What language will be used to write the first self aware program?
- Next by Date: Re: What language will be used to write the first self aware program?
- Previous by thread: Re: 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
|
|