如何在 SEQUENCE lisp 中输入?

How to take input in SEQUENCE lisp?

我正在尝试用 lisp 语言编写排序代码。 但我不知道该怎么做。 我想使用序列来使用 lisp 的排序功能。 我正在尝试创建这样的序列-

(setq arr (make-sequence '(vector integer) 10   :initial-element (read)))
(terpri)
(write arr)

这会将初始元素设置为用户输入。但是我想听取用户的意见,但我不知道该怎么做。任何建议都会有所帮助。

您可以使用 MAKE-ARRAY. You can set its fill-pointer to 0, and then use a loop to add 10 numbers to it with VECTOR-PUSH:

创建向量
(let ((vec (make-array 10 :fill-pointer 0)))
  (dotimes (i 10 vec)
    (format *query-io* "Number ~d/10: " (1+ i))
    (finish-output *query-io*)
    (vector-push (parse-integer (read-line *query-io*)) vec)))

你也可以这样实现:

(make-array 10 :initial-contents
            (loop
               for i from 1 to 10
               do (format *query-io* "Number ~d/10: " i)
               do (finish-output *query-io*)
               collecting (parse-integer (read-line *query-io*))))

但这会为输入创建一个临时列表。

此外,正如 Svante 所建议的,您可以使用 MAP-INTO:

;; If you don't want the number in the prompt, you can remove the LET
(let ((i 0))
  (map-into (make-array 10)
            (lambda () 
              (format *query-io* "Number ~d/10: " (incf i))
              (finish-output *query-io*)
              (parse-integer (read-line *query-io*)))))