Io 使用CL从终端进行简单读取

Io 使用CL从终端进行简单读取,io,terminal,common-lisp,Io,Terminal,Common Lisp,我是一名学生,对Common Lisp相当陌生。到目前为止,我只编写了一些应用程序,它们在自然语言上执行计算,从文本文件语料库中读取数据。现在我试图理解IO与终端在CL中的工作原理。为此,我试图编写一个超级简单的应用程序,从用户那里获取输入,并将其打印回屏幕。这就是我所拥有的: (defun main () (with-open-stream stream *terminal-io* :direction :output) (print (read-line stream nil))

我是一名学生,对Common Lisp相当陌生。到目前为止,我只编写了一些应用程序,它们在自然语言上执行计算,从文本文件语料库中读取数据。现在我试图理解IO与终端在CL中的工作原理。为此,我试图编写一个超级简单的应用程序,从用户那里获取输入,并将其打印回屏幕。这就是我所拥有的:

(defun main ()
  (with-open-stream stream *terminal-io* :direction :output)
    (print (read-line stream nil)))

然而,这给了我一个错误,说我需要使用断开信号,我不知道怎么做。你能帮我理解我的代码有什么问题吗(为什么)?提前感谢。

如果要从CLI读取输入,可以使用*stream

请记住
read
使用lisp读取器,这可能是一个优势。如果您想阅读一般文本,则应使用
readline


*:我不确定终端io和查询io之间的区别,也不确定它们在此上下文中是否可以互换。

如果要从CLI读取输入,可以使用*stream

请记住
read
使用lisp读取器,这可能是一个优势。如果您想阅读一般文本,则应使用
readline


*:我不确定终端io和查询io之间的区别,也不确定它们在这种情况下是否可以互换。

我认为您可能还希望使用通用lisp编写shell脚本。在这种情况下,您将需要shebang行。根据您的lisp实现,我通常使用SBCL和slimen。您有一个表:

然后我认为,既然你是一名学生,你想学习,我建议你检查asdf和uiop,作为sbcl中的扩展导入,看看这篇文章:

有了这个,我写了这个脚本,展示了一个示例,为了将来还可以探索uiop:run-program;-),使用不同的streams标准输入和终端io,您还应该了解常见的lisp流

#!/usr/bin/sbcl --script

(require :uiop)

;;reading comman line arguments
(dolist (element uiop:*command-line-arguments*)
  (uiop:writeln element)
  (write-line element));; look the different output

;; for printing strings on *standard-output*
(write-line "Hi this is a sample script")
(format t "~A ~A ~A ~B ~%" "what you want even a number like" 2 "in binary" 2 )

;; now let's read other arguments
(write-line "please write some thing")

(defparameter line (read-line))

(write-line "you write a:")

(format t "~A" (type-of line))
(write-line "and this contents: ")
(write-line line)

;; now lets use *terminal-io*
(write-line "please introduce another thing" *terminal-io*)

(defparameter line (read-line))

(write-line "you write a:")

(format *terminal-io* "~A" (type-of line))
(write-line "and this contents: ")
(write-line line *terminal-io*)
当我执行它时:

╭─anquegi@toshiba-debian  ~/learn/lisp/StackOverFlow ‹ruby-2.2.1@laguna› 
╰─$ ./fromterminal.lisp a b c                                                                                                                   148 ↵
"a"
a
"b"
b
"c"
c
Hi this is a sample script
what you want even a number like 2 in binary 10 
please write some thing
calimero
you write a:
(SIMPLE-ARRAY CHARACTER (8))and this contents: 
calimero
please introduce another thing
pato
you write a:
(SIMPLE-ARRAY CHARACTER (4))and this contents: 
pato
还有更多将common lisp用作shell脚本的示例,如下所示:


,但我建议您探索和学习sbcl、asdf和uiop,祝您好运

我认为您可能还希望使用common lisp编写shell脚本。在这种情况下,您将需要shebang行。根据您的lisp实现,我通常使用sbcl和slimen。您有一个表:

然后我认为,既然你是一名学生,你想学习,我建议你检查asdf和uiop,作为sbcl中的扩展导入,看看这篇文章:

有了这个,我写了这个脚本,展示了一个示例,为了将来还可以探索uiop:run-program;-),使用不同的streams标准输入和终端io,您还应该了解常见的lisp流

#!/usr/bin/sbcl --script

(require :uiop)

;;reading comman line arguments
(dolist (element uiop:*command-line-arguments*)
  (uiop:writeln element)
  (write-line element));; look the different output

;; for printing strings on *standard-output*
(write-line "Hi this is a sample script")
(format t "~A ~A ~A ~B ~%" "what you want even a number like" 2 "in binary" 2 )

;; now let's read other arguments
(write-line "please write some thing")

(defparameter line (read-line))

(write-line "you write a:")

(format t "~A" (type-of line))
(write-line "and this contents: ")
(write-line line)

;; now lets use *terminal-io*
(write-line "please introduce another thing" *terminal-io*)

(defparameter line (read-line))

(write-line "you write a:")

(format *terminal-io* "~A" (type-of line))
(write-line "and this contents: ")
(write-line line *terminal-io*)
当我执行它时:

╭─anquegi@toshiba-debian  ~/learn/lisp/StackOverFlow ‹ruby-2.2.1@laguna› 
╰─$ ./fromterminal.lisp a b c                                                                                                                   148 ↵
"a"
a
"b"
b
"c"
c
Hi this is a sample script
what you want even a number like 2 in binary 10 
please write some thing
calimero
you write a:
(SIMPLE-ARRAY CHARACTER (8))and this contents: 
calimero
please introduce another thing
pato
you write a:
(SIMPLE-ARRAY CHARACTER (4))and this contents: 
pato
还有更多将common lisp用作shell脚本的示例,如下所示:

,但我建议您探索和学习sbcl、asdf和uiop,祝您好运,简单地说:

  • 您可以使用
    readline
    和friends读取标准输入,通常不需要额外的参数。因此,
    (读取行)
    从stdin返回一行。小心阅读,这是一种安全漏洞

  • 您可以使用
    write line
    和friends(无附加参数)以及
    (格式t…
    )写入标准输出

  • 当您想要在用户和程序之间建立对话时,您不需要使用标准的输入和输出,而是使用
    *查询io*
    作为
    读行
    写行
    等的可选参数。请注意,输出可以缓冲,因此在可移植代码中,您应该执行
    (完成输出*查询io*)
    (强制输出*查询io*)
    在预期用户输入之前刷新输出

简单地说:

  • 您使用
    read line
    和friends读取标准输入,通常没有附加参数。因此,
    (read line)
    从stdin返回一行。注意
    read
    ,这是一种安全漏洞

  • 您可以使用
    write line
    和friends(无附加参数)以及
    (格式t…
    )写入标准输出

  • 当您想要在用户和程序之间建立对话时,您不需要使用标准的输入和输出,而是使用
    *查询io*
    作为
    读行
    写行
    等的可选参数。请注意,输出可以缓冲,因此在可移植代码中,您应该执行
    (完成输出*查询io*)
    (强制输出*查询io*)
    在预期用户输入之前刷新输出


您正在尝试从一个流中读取数据,您已经打开了该流以进行输出…?但是
with open stream
表单中的语法也完全错误。您也不清楚为什么要打开一个流。通常,
*终端io*
流已经打开。您可以直接使用它…不需要打开一个流。您正在尝试从一个流中读取,您已经打开以进行输出…?但是
with open stream
表单中的语法也完全错误。您为什么要打开一个流也不清楚。通常
*terminal io*
流已经打开。您可以直接使用它…没有必要打开一个流。这是一个非常好的耐心回答。干杯!这是一个非常有耐心的回答。干杯!