0
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 ? */

alinsoar
  • 15,386
  • 4
  • 57
  • 74
Marcos
  • 111
  • 1
  • 2
  • 5
  • Start with the formatting. Put the procedure names immediately after the opening paren (no space). Don't follow procedure names immediately with an open paren. That might get you started. Still problems though. – clartaq Apr 21 '22 at 15:25
  • To learn Scheme, this [course](https://learning.edx.org/course/course-v1:UBCx+HtC1x+2T2017/home) is strongly recommended (The BSL language is actually essentially Scheme) – mnemenaut Apr 21 '22 at 15:39
  • You need to study the Scheme syntax in more detail. Scheme is very dissimilar to the curly-brace languages you're used to. (You will probably find, like many others have, that it starts to make sense once you *stop* laying it out as if it were a curly-brace language.) – molbdnilo Apr 22 '22 at 08:58
  • you do `cat input.scm | mit-scheme` or `mit-scheme < input.scm`. There are also other schemes. – alinsoar Apr 22 '22 at 13:09

2 Answers2

1

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
> 
mnemenaut
  • 684
  • 2
  • 11
  • but the function just input three parameters ,why " 1" not a proedure application.Can I use other way to code the function? – Marcos Apr 21 '22 at 23:49
  • Your idea for coding the function is correct, but _every_ function call in Scheme is `(procedure argument ... )`. `1` is a number, `bigger` is a procedure, `(bigger 1 2)` applies bigger to arguments 1 and 2, producing result 2. – mnemenaut Apr 22 '22 at 06:03
  • Chez is an excellent Scheme implementation, but Racket (with the DrRacket IDE) is better for _learning_ Scheme. Even if one has previous programming experience, I strongly recommend the [How to Code](https://learning.edx.org/course/course-v1:UBCx+HtC1x+2T2017/home) course for getting started. – mnemenaut Apr 22 '22 at 06:12
  • How can I rewrite the code to running the .ss file in terminal ? @mnemenaut – Marcos Apr 22 '22 at 07:08
  • @Marcos: Added terminal examples to answer; I use these with Chez, but also develop code in DrRacket with built-in tests like `(check-expect (bigger-sum-of-squares 1 3 7) 58)` – mnemenaut Apr 22 '22 at 08:54
  • I found incorrent : (bigger (smaller (a b) c))) => (bigger (smaller a b) c))), then compile : chez xx.ss ,it's can running, [Scheme] chez bigger.ss 22:48:34 ☁ test ☂ ✖ ✭ Chez Scheme Version 9.5.6 Copyright 1984-2021 Cisco Systems, Inc. >> ( bigger-sum-of-squares 1 2 3 ) 13 > – Marcos Apr 22 '22 at 14:57
0

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.

Marcos
  • 111
  • 1
  • 2
  • 5