How can I run scheme?

123 views Asked by At
1( import ( chezscheme ) )
2
3
4 ( define ( bigger a b )
5          ( if ( > a b )
6             a
7             b
8             )
9 )
10
11 ( define ( smaller a b )
12          ( if ( < a b )
13               a
14               b
15               )
16          )
17
18
19 ( define ( square x )  ( * x x  ) )
20
21
22
23
24 ( define ( sum-of-squares a b )
25          ( + ( square a ) ( square b ) )
26 )
27
28
29
30
31 ( define ( bigger-sum-of-squares a b c )
32          ( sum-of-squares ( bigger a b ) ( bigger( smaller( a b 
   ) )  c ) )
33 )
34
35
36  bigger-sum-of-squares( 1 3 7 )
37

/* I install Chez Scheme Version 9.5.6 When I use this command to compile : chez bigger.ss, it will happen: "Exception: attempt to apply non-procedure 1" I try many times ,but still not solve it , who can tell me where's bug ? */

2

There are 2 answers

6
mnemenaut On

Here is the code conventionally formatted, with corrected (probably) parentheses:

(define (bigger a b)
  (if (> a b)
      a
      b))

(define (smaller a b)
  (if (< a b)
      a
      b))

(define (square x)
  (* x x))

(define (sum-of-squares a b)
  (+ (square a) (square b)))

(define (bigger-sum-of-squares a b c)
  (sum-of-squares (bigger a b) (bigger (smaller a b) c)))

(bigger-sum-of-squares 1 3 7)

The error "Exception: attempt to apply non-procedure 1" results from bigger-sum-of-squares( 1 3 7 ) -- the Scheme processor interprets (1 3 7) as a procedure application (like (+ 3 7)), but 1 is not a procedure.

For testing, the code can be copied from an editor and pasted into a terminal:

% scheme
Chez Scheme Version 9.5.7.6
Copyright 1984-2021 Cisco Systems, Inc.

> (define (bigger a b)
    (if (> a b)  a  b ))
> (define (smaller a b)
    (if (< a b)  a  b ))
> (define (square x)
    (* x x))
> (define (sum-of-squares a b)
    (+ (square a) (square b)))
> (define (bigger-sum-of-squares a b c)
    (sum-of-squares (bigger a b) (bigger (smaller a b) c)))
> (bigger-sum-of-squares 1 3 7)
58
> 

...or the definitions in a file can be loaded into Chez Scheme's interaction environment:

% scheme          
Chez Scheme Version 9.5.7.6
Copyright 1984-2021 Cisco Systems, Inc.

> (load "bigger.ss")
> (bigger-sum-of-squares 1 3 7)
58
> 
0
Marcos On

enter image description here

I found it's can running ,when use chez bigger.ss , then running function :(bigger-sum-of-squares 1 2 3 ), it will appear the result.