有没有办法拒绝麻省理工学院计划的冗长?



我最近决定按照 SICP 中的示例开始使用 MIT Scheme。我从 Ubuntu 存储库安装了方案。

sudo apt-get install mit-scheme

给定一个如下所示的输入文件:

486
(+ 137 349)
(- 1000 334)
(* 5 99)
(/ 10 5)
(* 25 4 12)

我按如下方式运行方案。

scheme < Numbers.scm

它产生以下输出。

MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Image saved on Sunday February 7, 2016 at 10:35:34 AM
Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118 || Edwin 3.116
1 ]=> 486
;Value: 486
1 ]=> (+ 137 349)
;Value: 486
1 ]=> (- 1000 334)
;Value: 666
1 ]=> (* 5 99)
;Value: 495
1 ]=> (/ 10 5)
;Value: 2
1 ]=> (* 25 4 12)
;Value: 1200
1 ]=> 
End of input stream reached.
Moriturus te saluto.

这个输出感觉太多了,所以我目前正在像这样削减它。

scheme < Numbers.scm  | awk '/Value/ {print $2}
486
486
666
495
2
1200

有没有一种本机方法来减少方案的冗长程度,这样我就可以在不诉诸外部进程的情况下获得类似于上述输出的东西?

我已经检查了scheme --help的输出,但没有找到任何明显的选择。


请注意,将文件名作为参数传递似乎在 MIT 方案中不起作用。

scheme Numbers.scm  
MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Image saved on Sunday February 7, 2016 at 10:35:34 AM
Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118 || Edwin 3.116
;Warning: Invalid keyword: "Numbers.scm"
;Warning: Unhandled command line options: ("Numbers.scm")
1 ]=> 

你来了:

scheme --quiet < Numbers.scm 

现在,这将完全抑制 REPL,除非发生错误,以便不显示未显式显示的内容。 例如,评估(+ 2 3)返回5,但不打印,因为您没有告诉它打印。您需要使用display等程序来打印信息或返回使用REPL的唯一目的是显示您的结果。

我原本希望你能做到:

scheme --quiet --load Numbers.scm

但它不会在文件后退出,并且添加--eval (exit)REPL会询问您是否要退出。

编辑

(define (displayln v)
(display v)
(newline)
v)
(displayln (+ 4 5))
; ==> 9, in addition you get the side effect that "9n" is written to current output port

您也可以创建一个宏来执行此操作:

(define-syntax begin-display
(syntax-rules ()
((_ form ...) (begin (displayln form) ...))))
(begin-display
486
(+ 137 349) 
(- 1000 334)
(* 5 99)
(/ 10 5)
(* 25 4 12))
; ==> 1200. In addition you get the side effect that "486n486n666n49n2n1200n" is written to current output port

作为一种解决方法,

scheme < Numbers.scm | gawk '/^;Value: / { sub(/^;Value: /, ""); print }'

但也许您会将其作为脚本文件而不是 stdin 流运行?不确定 MIT 方案调用,例如

scheme Numbers.scm

虽然这样你必须用(display)或其他东西明确地打印出结果,否则它们不会被注意到。

最新更新