0

我对 LISP 真的很陌生。我正在使用 LispWorks,并且在如何接受来自键盘的字符串输入方面遇到问题。这是我下面的代码编译得很好,但是当我运行时,它给出了一个错误:

“读取流#<同义词流到 背景输入>时文件结束。”

(defun get-input (prompt)
  (clear-input)
  (write-string prompt)
  (finish-output)
  (let ((x (read-line)))
  (write-string x)
  (close x)))

(write-string (get-input "Enter a sentence: "))
(finish-output)

我已经尝试使用“读取”的各种代码无济于事,请有人帮忙。

4

1 回答 1

0

下面的代码解决了我的问题:

(defun split-by-one-space (string)
    (loop for i = 0 then (1+ j)
          as j = (position #\Space string :start i)
          collect (subseq string i j)
          while j))

(defun rev_string()
    (let ((mystr (read-line *query-io* NIL "")))
    (terpri)
        (format t "The Original String is : ~a~%" mystr)
    (terpri)
    (format t "The Reversed String By Character is : ~a~%" (reverse mystr))
    (terpri)    
    (format t "The Reversed String By Word is : ~a~%" (nreverse (split-by-one-space mystr)))))
(rev_string)
于 2022-01-23T14:06:21.167 回答