如何使教堂数字在 lisp 中更易于阅读?
我可以使用方案相当容易地定义教堂数字:
> (define f (lambda (x) x))
> (f f) ;0
#<procedure:f>
> (f (f f)) ;1
#<procedure:f>
但是,这并不容易识别 (ff)
是 0 且 (f (ff)) 是 1。有没有一种方法可以我可以让这些数字更易读吗?理想的情况是:
> (f f)
0
> (f (f f))
1
这个例子在方案中,但我会用任何 lisp 来回答。
I can define church numerals fairly easy using scheme:
> (define f (lambda (x) x))
> (f f) ;0
#<procedure:f>
> (f (f f)) ;1
#<procedure:f>
However, this doesn't make it very easy to recognize that (f f)
is 0 and (f (f f)) is 1. Is there a way that I can make these numerals more readable? What would be ideal is this:
> (f f)
0
> (f (f f))
1
The example is in scheme, but I'll take an answer in any lisp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先让我们定义真正的教堂数字,它具有
0 != 1
的理想属性:因此
zero
是 0 的教堂表示,(succ Zero)
1,(succ (succ 零))
2 等等。现在,由于这些只是函数,因此无法告诉 repl 将它们显示为数字,但您可以定义一个函数 cn-to-int ,它将教堂数字转换为整数,然后可以正常显示:
First let's define real church numerals which have the desirable property that
0 != 1
:So
zero
is the church representation of 0,(succ zero)
of 1,(succ (succ zero))
of 2 and so on.Now since those are just functions, there is no way to tell the repl to display them as numbers, but you can define a function cn-to-int which converts church-numerals to ints which can then be displayed normally: