Sujet : Re: list formats
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 01. Jul 2025, 00:10:00
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103v5fp$2ebqi$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
I think the way I posed the question was too complicated in latter
postings (under the name loop problem) so I try to samplify it:
>
we have this list:
>
(("car ((noun automoblie sks hui)(noun vehicle sks hui)) is ((verb hji
ska) (noun ako hui)) blue ((adj hji hui))" "strong ((adj hji hui) (adv
ly hui)) is ((verb hji ska) (verb kos hji)) man ((noun ako hui) (prop
hui))"))
>
>
and want to convert it to this format:
>
(("car" ((noun automoblie sks hui)(noun vehicle sks hui))
"is" ((verb hji ska) (noun ako hui))
"blue" ((adj hji hui)))
("strong" ((adj hji hui) (adv ly hui))
"is" ((verb hji ska) (verb kos hji))
"man" ((noun ako hui) (prop hui))))
Here's my solution. It's untested and doesn't do any error checking.
(defun parse-my-list (list)
(loop for string in (car list)
collect (parse-my-string string)))
Why didn't he say
(mapcar #'parse-my-string (car list))
It seems that he has no affinity whatsoever for Lispy
programming. Worshippers of LOOP never do.
(defun parse-my-string (string)
"Parse a string of repeating "<name> <list-of-attributes> ..."
A quote within quotes.
(loop with end = (length string)
with last-end
for start = 0 then last-end
while (start < end)
Infix?
Worshippers of LOOP have no affinity whatsoever for Lispy
programming.
collect (let* ((space-pos (position #\space string :start start))
(word (subseq string start space-pos)))
(setq start (1+ space-pos))
word)
collect (multiple-value-bind (list end-pos)
(read-from-string string t nil :start start)
;; skip over whitespace after the list
(setq last-end
(position #\space string :start end-pos :test #'char/=))
list)))
There are two ways of constructing a software design: One way is
to make it so simple that there are obviously no deficiencies,
and the other way is to make it so complicated that there are no
obvious deficiencies. --- Charles Antony Richard Hoare
Common Lisp is a significantly ugly language. --- Dick Gabriel
The good news is, it's not Lisp that sucks, but Common Lisp.
--- Paul Graham
Gauche Scheme
(define sentences
'(("car ((noun automoblie sks hui)(noun vehicle sks hui))
is ((verb hji ska) (noun ako hui))
blue ((adj hji hui))"
"strong ((adj hji hui) (adv ly hui))
is ((verb hji ska) (verb kos hji))
man ((noun ako hui) (prop hui))")))
Using a collector named "res".
(define (parse-sentence input)
(with-input-from-string input
(lambda ()
(do. ((res (mlistbag))
(x (read) <>)) ;; <> means use the preceding expression.
((eof-object? x) (res))
(res (if (pair? x) x (symbol->string x)))))))
(define (parse-list input)
(map parse-sentence (car input)))
(parse-list sentences)
===>
(("car" ((noun automoblie sks hui) (noun vehicle sks hui))
"is" ((verb hji ska) (noun ako hui))
"blue" ((adj hji hui)))
("strong" ((adj hji hui) (adv ly hui))
"is" ((verb hji ska) (verb kos hji))
"man" ((noun ako hui) (prop hui))))
Given:
(define-syntax do.-aux
(syntax-rules ( <> )
[ (do.-aux ((v init <>) more ...) seen stuff ...)
(do.-aux ((v init init) more ...) seen stuff ...) ]
[ (do.-aux (what more ...) (seen ...) stuff ...)
(do.-aux (more ...) (seen ... what) stuff ...) ]
[ (do.-aux () seen stuff ...)
(do seen stuff ...) ] ))
(define-syntax do.
(syntax-rules ()
[ (do. things more ...)
(do.-aux things () more ...) ] ))
(define (mbag init func :optional (pass-through #f))
(let ((val init) (func func) (pass-through pass-through))
(lambda args
(if (null? args)
val
(begin
(set! val
;; A "kons" may have been supplied.
((if (null? (cdr args)) func (cadr args))
(car args) val))
(if pass-through
(car args)
val))))))
(define (mlistbag :optional (pass-through #t))
(let ((bag (mbag '() cons pass-through)))
(lambda args
(if (null? args)
(reverse (bag))
(apply bag args)))))