Hello,
For my simple text manipulation program, I need to pass elements of
an assoc list (by assoc list, incase that's not the right term, I
mean a list of the form '((a . b) (c . d))) to a function that takes
only strings. I found, from my Lisp interpreter that (string
'my-symbol) => "my-symbol" and so that's the function I use.
I have an assoc list that I am iterating over using dolist. Since
each element during the iteration (the first argument to dolist)
will be of the form (foo . bar), I figured that
(string-replace foo (string (cdr (assoc elem my-assoc-list))))
Will be a valid call since the (cdr (assoc ... should return a
symbol. From CLISP I get:
*** - STRING: argument (BODY . "<foo>") should be a string, a symbol
or a character
Why is this? Incase the problem stems from another part (although
the error message seems to narrow it down pretty clearly to the
string call) I pasted two relevant snippets of my program.
;; *tag-to-content* is the assoc list.
(defun insert-user-tags (html)
(let ((new-html "")
(start-tag "<:")
(end-tag ":>"))
(dolist (elem *tag-to-content*)
(setf elem-tag (concatenate 'string start-tag (string elem) end-tag))
(setf new-html
(concatenate 'string new-html
(string-replace
elem-tag
(string (cdr (assoc elem *tag-to-content*)))
new-html))))))
;; From ODCL-1.1.7 by OnShore Software
(defun string-replace (char replace-text string)
(let ((count (char-count char string)))
(if (= 0 count)
(copy-seq string)
(let* ((orig-length (length string))
(repl-length (length replace-text))
(new-length (+ orig-length (* (- repl-length 1) count)))
(new-string (make-string new-length))
(j 0))
(do ((i 0 (incf i)))
((= i orig-length) new-string)
(let ((test-char (aref string i)))
(cond ((char= test-char char)
(overlay new-string replace-text j)
(incf j repl-length))
(t
(setf (aref new-string j) test-char)
(incf j)))))))))
Thanks a lot, I appreciate the help!
--
Jordan Katz <····@underlevel.net> | Mind the gap
In article <··············@underlevel.underlevel.net>, Jordan Katz wrote:
[snip]
>
> I have an assoc list that I am iterating over using dolist. Since
> each element during the iteration (the first argument to dolist)
> will be of the form (foo . bar), I figured that
>
> (string-replace foo (string (cdr (assoc elem my-assoc-list))))
>
> Will be a valid call since the (cdr (assoc ... should return a
> symbol. From CLISP I get:
>
> *** - STRING: argument (BODY . "<foo>") should be a string, a symbol
> or a character
>
>
> Why is this? Incase the problem stems from another part (although
> the error message seems to narrow it down pretty clearly to the
> string call) I pasted two relevant snippets of my program.
It's the /other/ STRING call :-)
> ;; *tag-to-content* is the assoc list.
> (defun insert-user-tags (html)
> (let ((new-html "")
> (start-tag "<:")
> (end-tag ":>"))
> (dolist (elem *tag-to-content*)
> (setf elem-tag (concatenate 'string start-tag (string elem) end-tag))
^^^^^^^^^^^^
Also note that the argument (HTML) isn't used anywhere in this
function. And elem-tag is meant to be special? I guess not...
> (setf new-html
> (concatenate 'string new-html
> (string-replace
> elem-tag
> (string (cdr (assoc elem *tag-to-content*)))
You are already looping over *tag-to-content*. If *tag-to-content*
is an alist, elem is a cons cell. So, this call to ASSOC is
probably wrong.
> new-html))))))
[snip]
BTW, there is also
CL-USER 26 > (loop for (car . cdr) in '((a . b) (c . d)) do
(pprint (list 'blark car cdr)))
(BLARK A B)
(BLARK C D)
NIL
Regards,
--
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."
PGP key ID 0x42B32FC9