Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does `if` statement work in Scheme?

Tags:

scheme

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, enter image description here

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,

like image 547
Chan Avatar asked Feb 24 '23 10:02

Chan


2 Answers

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).

like image 164
Brian Campbell Avatar answered Mar 06 '23 02:03

Brian Campbell


What version of Scheme are you using? DrScheme 372 has no problem with an if statement without an else clause.

In any case, Scheme provides the when and unless operators, which act like an if statement that has (respectively) only a then-branch or only an else-branch. Given that, there's no pressing need for the if statement to have its else-branch be optional.

like image 34
Lily Ballard Avatar answered Mar 06 '23 00:03

Lily Ballard