什么是交互式 REPL IO 功能?
what is the interactive REPL IO function?
学习Common Lisp有一段时间了,遇到一个问题
我如何实现这样一个允许用户输入一些单词直到用户输入退出的功能。
(其实我想知道什么样的命令行交互函数API才符合这样的要求)
例如
在 REPL 中提示 "please input a word: ",然后将用户输入存储到全局 my-words 中,当用户输入 "exit".
时退出
您的说明有点不完整(例如,您的问题中的单词由什么组成?如果用户添加多个单词怎么办?如果输入为空怎么办?)。下面我使用 CL-PPCRE 将输入拆分为不同的单词并一次添加它们,因为它通常看起来很有用。在您的情况下,您可能想要添加更多错误检查。
如果您想与用户交互,您应该从 *QUERY-IO*
流中读取和写入。在这里,我将按照您的要求展示一个带有全局变量的版本,以及另一个没有副作用的版本(input/output 除外)。
使用全局变量
定义全局变量并用一个空的可调数组对其进行初始化。
我使用的是数组,这样可以很容易地在末尾添加单词,但您也可以使用队列。
(defvar *my-words* (make-array 10 :fill-pointer 0 :adjustable t))
以下函数改变了全局变量:
(defun side-effect-word-repl ()
(loop
(format *query-io* "~&Please input a word: ")
(finish-output *query-io*)
(let ((words (ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
(read-line *query-io*))))
(dolist (w words)
(when (string-equal w "exit") ; ignore case
(return-from side-effect-word-repl))
(vector-push-extend w *my-words*)))))
LOOP
uses the simple syntax where there are only expressions and no loop-specific keywords. I first write the prompt to *QUERY-IO*
. The ~&
FORMAT
directive performs the same operation as FRESH-LINE
. As Rainer pointed out in comments, we have to call FINISH-OUTPUT
确保消息在用户预期回复之前有效打印。
然后,我从同一个双向流中读取整行,并将其拆分为一个单词列表,其中一个单词是一串非空白字符。
使用 DOLIST
, I iterate over the list and add words into the global array with VECTOR-PUSH-EXTEND
. But as soon as I encouter "exit"
, I terminate the loop; since I rely on STRING-EQUAL
,测试不区分大小写。
无副作用的方法
不鼓励像上面那样使用全局变量。如果你只需要提示其中有returns个单词列表,那么下面的就够了。在这里,我使用 PUSH
/NREVERSE
成语来构建结果单词列表。
(defun pure-word-repl ()
(let ((result '()))
(loop
(format *query-io* "~&Please input a word: ")
(finish-output *query-io*)
(let ((words (ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
(read-line *query-io*))))
(dolist (w words)
(when (string-equal w "exit")
(return-from pure-word-repl (nreverse result)))
(push w result))))))
关于单词的注意事项
正如 jkiiski 评论的那样,在 :word-boundary
处拆分单词可能会更好。我尝试了不同的组合,以下结果似乎对奇怪的示例字符串很满意:
(mapcan (lambda (string)
(ppcre:split :word-boundary string))
(ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
"amzldk 'amlzkd d;:azdl azdlk"))
=> ("amzldk" "'" "amlzkd" "d" ";:" "azdl" "azdlk")
我首先删除所有空格并将字符串拆分为一个字符串列表,其中可以包含标点符号。然后,每个字符串本身在 :word-boundary
处拆分,并与 MAPCAN
连接以形成单独单词的列表。但是,我真的无法猜测您的实际需求是什么,因此您可能应该定义自己的 SPLIT-INTO-WORDS
函数来验证和拆分输入字符串。
CL-USER 23 > (progn
(format t "~%enter a list of words:~%")
(finish-output)
(setf my-words (read))
(terpri))
enter a list of words:
(foo bar baz)
或
CL-USER 28 > (loop with word = nil
do
(format t "~%enter a word or exit:~%")
(finish-output)
(setf word (read))
(terpri)
until (eql word 'exit)
collect word)
enter a word or exit:
foo
enter a word or exit:
bar
enter a word or exit:
baz
enter a word or exit:
exit
(FOO BAR BAZ)
学习Common Lisp有一段时间了,遇到一个问题 我如何实现这样一个允许用户输入一些单词直到用户输入退出的功能。 (其实我想知道什么样的命令行交互函数API才符合这样的要求)
例如 在 REPL 中提示 "please input a word: ",然后将用户输入存储到全局 my-words 中,当用户输入 "exit".
时退出您的说明有点不完整(例如,您的问题中的单词由什么组成?如果用户添加多个单词怎么办?如果输入为空怎么办?)。下面我使用 CL-PPCRE 将输入拆分为不同的单词并一次添加它们,因为它通常看起来很有用。在您的情况下,您可能想要添加更多错误检查。
如果您想与用户交互,您应该从 *QUERY-IO*
流中读取和写入。在这里,我将按照您的要求展示一个带有全局变量的版本,以及另一个没有副作用的版本(input/output 除外)。
使用全局变量
定义全局变量并用一个空的可调数组对其进行初始化。 我使用的是数组,这样可以很容易地在末尾添加单词,但您也可以使用队列。
(defvar *my-words* (make-array 10 :fill-pointer 0 :adjustable t))
以下函数改变了全局变量:
(defun side-effect-word-repl ()
(loop
(format *query-io* "~&Please input a word: ")
(finish-output *query-io*)
(let ((words (ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
(read-line *query-io*))))
(dolist (w words)
(when (string-equal w "exit") ; ignore case
(return-from side-effect-word-repl))
(vector-push-extend w *my-words*)))))
LOOP
uses the simple syntax where there are only expressions and no loop-specific keywords. I first write the prompt to*QUERY-IO*
. The~&
FORMAT
directive performs the same operation asFRESH-LINE
. As Rainer pointed out in comments, we have to callFINISH-OUTPUT
确保消息在用户预期回复之前有效打印。然后,我从同一个双向流中读取整行,并将其拆分为一个单词列表,其中一个单词是一串非空白字符。
使用
DOLIST
, I iterate over the list and add words into the global array withVECTOR-PUSH-EXTEND
. But as soon as I encouter"exit"
, I terminate the loop; since I rely onSTRING-EQUAL
,测试不区分大小写。
无副作用的方法
不鼓励像上面那样使用全局变量。如果你只需要提示其中有returns个单词列表,那么下面的就够了。在这里,我使用 PUSH
/NREVERSE
成语来构建结果单词列表。
(defun pure-word-repl ()
(let ((result '()))
(loop
(format *query-io* "~&Please input a word: ")
(finish-output *query-io*)
(let ((words (ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
(read-line *query-io*))))
(dolist (w words)
(when (string-equal w "exit")
(return-from pure-word-repl (nreverse result)))
(push w result))))))
关于单词的注意事项
正如 jkiiski 评论的那样,在 :word-boundary
处拆分单词可能会更好。我尝试了不同的组合,以下结果似乎对奇怪的示例字符串很满意:
(mapcan (lambda (string)
(ppcre:split :word-boundary string))
(ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
"amzldk 'amlzkd d;:azdl azdlk"))
=> ("amzldk" "'" "amlzkd" "d" ";:" "azdl" "azdlk")
我首先删除所有空格并将字符串拆分为一个字符串列表,其中可以包含标点符号。然后,每个字符串本身在 :word-boundary
处拆分,并与 MAPCAN
连接以形成单独单词的列表。但是,我真的无法猜测您的实际需求是什么,因此您可能应该定义自己的 SPLIT-INTO-WORDS
函数来验证和拆分输入字符串。
CL-USER 23 > (progn
(format t "~%enter a list of words:~%")
(finish-output)
(setf my-words (read))
(terpri))
enter a list of words:
(foo bar baz)
或
CL-USER 28 > (loop with word = nil
do
(format t "~%enter a word or exit:~%")
(finish-output)
(setf word (read))
(terpri)
until (eql word 'exit)
collect word)
enter a word or exit:
foo
enter a word or exit:
bar
enter a word or exit:
baz
enter a word or exit:
exit
(FOO BAR BAZ)