`if` 语句在 Scheme 中是如何工作的? [英] How does `if` statement work in Scheme?

查看:50
本文介绍了`if` 语句在 Scheme 中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前自学Scheme的链接,(有时称为functional) 编程风格,其中您不依赖副作用,而是仅通过应用函数来计算值并从它们返回结果.在 applicative 风格中,没有副作用,语句的唯一结果是返回一个值.一个 if 语句在它的一个分支中没有返回值在这种情况下没有意义;它没有用,事实上,当您尝试使用该 if 语句返回的值时,它会导致未定义的行为.在应用风格中,语言中的每个语句都是一个表达式,根据它计算的值进行评估,而不是它引起的副作用.

如果您使用 display 来提供输出,那么您并不是在使用纯粹的应用风格.这完全没问题,但许多对 Scheme 的介绍都喜欢从呈现应用风格开始,因为这样更容易推理和了解一切实际上是如何工作的.

然而,由于您不是在使用假定编程风格的文本进行工作,因此我建议您选择不同的方言.在语言"菜单下,选择选择语言",然后我建议选择R5RS"(旨在尽可能接近标准)或相当大"(即 R5RS 加上一堆方便的 PLT 扩展).

This is the link that I'm current teaching myself Scheme, http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-1.html

According to the author,

Then I tried a minimal example

(define (check-p x)
  (if (>= x 0)
      (display 1)))

, and DrScheme gave me errors:

if: bad syntax (must have an "else" expression) in: (if (>= x 0) (display 1))

If I put an extra statement within the if statement, then it works. But I don't get it, why do we need an extra statement there? The code statement above made sense to me. If the number is greater than 0, display a 1, otherwise do nothing. Any idea?

Thanks,

解决方案

DrScheme includes several "teaching" dialects of Scheme which are limited subsets of (they impose more restrictions than) standard R5RS or R6RS Scheme. The dialect that you are using probably restricts you to using if statements in which you provide values for both branches. In fact, I checked just now, and it looks like all of the "teaching" dialects forbid you from using an if statement with only one branch.

This is intended to help you learn to program in an applicative (sometimes known as functional) style of programming, in which you do not rely upon side effects but instead compute values merely by applying functions and returning results from them. In the applicative style, with no side effects, the only result of a statement is to return a value. An if statement that does not return a value in one of its branches would have no meaning for that case; it would not be useful, and in fact would cause undefined behavior when you tried to use the value returned by that if statement. In the applicative style, every statement in the language is an expression, evaluated for the value it computes, not the side effects that it causes.

If you are using display to provide output, you are not using a purely applicative style. That's perfectly fine, but many introductions to Scheme like to start by presenting an applicative style, because it is much easier to reason about and learn how everything actually works.

Since you're not working from a text that assumes an applicative style of programming, however, I'd recommend choosing a different dialect. Under the "Language" menu, select "Choose Language", and then I would recommend choosing either "R5RS" (which is intended to be as close to the standard as possible), or "Pretty Big" (which is R5RS plus a bunch of handy PLT extensions).

这篇关于`if` 语句在 Scheme 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆