scheme - 通过在 Scheme 中折叠来获取列表的“与”
问题描述
在
H. Abelson 和 GJ Sussman 与 J. Sussman 合着的《计算机程序的结构和解释accumulation
》一书中,或fold-right
在第 2.2.3 节中介绍如下:
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
我尝试使用它来获取and
布尔变量列表,方法是:
(accumulate and
true
(list true true false))
但是,这给了我and: bad syntax
DrRacket 中的错误(带有#lang sicp
),我不得不这样做:
(accumulate (lambda (x y) (and x y))
true
(list true true false))
为什么?我相信这与and
特殊形式的方式有关,但是我对Scheme的了解还不够多。也许我只是错过了一些明显的错误......
解决方案
您回答了自己的问题:and
是具有特殊评估规则的特殊形式(不是正常过程!),并且accumulate
需要正常过程,因此您需要将其包装在过程中。
要了解为什么and
是特殊形式,请考虑以下示例,这些示例证明and
需要特殊的评估规则(与过程不同),因为它在发现错误值时会短路:
; division by zero never gets executed
(and #f (/ 1 0))
=> #f
; division by zero gets executed during procedure invocation
((lambda (x y) (and x y)) #f (/ 1 0))
=> /: division by zero
推荐阅读
- java - 具体如何获取
基于Java HashMap中给定键的条目? - c++ - Visual Studio Code 中的“特征/密集:没有这样的文件或目录”
- otrs - 如何在otrs系统上更改域
- mysql - 仅显示产品不存在的仓库 - 将表乘以 mysql
- mysql - 获取多个日期的每月计数
- python - 返回公会所有者用户名 discord.py 的 Discord 机器人
- python - Python 3.9.1(或 3.9.2)中“subprocess”模块的问题 - “subprocess.Popen”不起作用
- java - 如何在 http 流的末尾发送错误代码
- python - 一次为多个参数运行 AWS Lambda
- css - chrome macOS 水平滚动条无论如何都保持隐藏状态(尽管溢出:滚动)