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,
asked Apr 21, 2011 at 23:58 13.7k 42 42 gold badges 109 109 silver badges 156 156 bronze badges hmmm, works in the online repl: sisc-scheme.org/sisc-online.php Commented Apr 22, 2011 at 0:10Trying to learn from the scheme-in-fixnum-days text is not a good idea, why didn't you start from HtDP instead?
Commented Apr 22, 2011 at 2:31 See also "Why is one-armed if missing from Racket": stackoverflow.com/questions/10863192/… Commented Jun 3, 2012 at 19:42DrScheme includes several "teaching" dialects of Scheme which are limited subsets of (they impose more restrictions than) standard R 5 RS or R 6 RS 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).