Are circuits functions of a single liner?

I follow the Framework and Interpretation of computer programs, and trying to solve the Ex 1.3 problem, I came to the following code as my first attempt:

(define (sumsq a b c)(
(define highest (if (> (if (> a b) a b) (if (> a c) a c)) (if (> a b) a b) (if (> a c) a c)))
(define second_h (if (> (if (> a b) b a) (if (> a c) c a)) (if (> a b) b a) (if (> a c) c a)))
(+ (* highest highest) (* second_h second_h)))

It did not work, and I looked for a solution and found them in the SICP Wiki

;; ex 1.3
;; implemented using only techniques covered to this point

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

(define (sum-of-squares x y)
  (+ (square x) (square y)))

(define (largest-two-of-three x y z)
  (if (>= x y)
      (sum-of-squares x (if (>= y z) y z))
      (sum-of-squares y (if (>= x z) x z))))

The difference was that I used several operators to define the variables, and then summed the squares, while the correct way is to define each of my lines as functions.

Are the functions in the circuit a single liner? Or did I miss it all?

+3
source share
5 answers

What you wrote (minus one extra pair) is:

(define (sumsq a b c)
  (define highest
    (if (> (if (> a b) a b)
           (if (> a c) a c))
      (if (> a b) a b)
      (if (> a c) a c)))
  (define second_h
    (if (> (if (> a b) b a)
           (if (> a c) c a))
      (if (> a b) b a)
      (if (> a c) c a)))
  (+ (* highest highest) (* second_h second_h)))

, , . (+ (* a a) (* b b)) , , , , , .

, , , , (...) , . , , : (if (> a b) a b) (if (> a b) b a). , :

(define (min a b) (if (< a b) a b))
(define (max a b) (if (< a b) b a))

, :

(define (sumsq a b c)
  (define highest
    (if (> (max a b) (max a c))
      (max a b)
      (max a c)))
  (define second_h
    (if (> (min a b) (min a c))
      (min a b)
      (min a c)))
  (+ (* highest highest) (* second_h second_h)))

:

(define (sumsq a b c)
  (define highest
    (max (max a b) (max a c)))
  (define second_h
    (max (min a b) (min a c)))
  (+ (* highest highest) (* second_h second_h)))

, , (max (max a b) (max a c)) - , , a b c, (max (max a b) c). second_h, , . , a ?

, , x y. if x < y, , y , , . , , x z, , . , y < x.

+5

, . :

(define (sumsq a b c)
  ((define highest 
     (if (> (if (> a b) a b)
            (if (> a c) a c))
         (if (> a b) a b)
         (if (> a c) a c)))
   (define second-h
     (if (> (if (> a b) b a)
            (if (> a c) c a))
         (if (> a b) b a)
         (if (> a c) c a)))
   (+ (* highest highest)
      (* second-h second-h)))

, : ; , . , . , , , - . , , , , .

. , SICP , , , . .

: . if , . , , , .

, . - , , , , . , , . , , , , .

+6

- bottom-up programming, . .

, . , .

+4

: (define (func param) (define...) (define...))

: (define (func param) body)

- ... , . , . , , .

" ?" "", : (begin (+ 1 1) (+ 2 2)) = > 4

(+ 1 1) , , , .

, ( let ) . :

  (let ((x 1))
    (+ 1 1)
    (+ 2 2))

. .

, , , - . Scheme, : " , , ..." , , . , , Scheme , .

+4

1.3 , . ​​, square, max min, , .

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

(define (max x y)
   (if (> x y) x y))

(define (min x y)
   (if (< x y) x y))

(define (sum-of-highest-squares x y z)
   (+ (square (max x y))
      (square (max (min x y) z))))

sum-of-highest-squares , x y ( ) ( x y, , ) z.

: SICP 1.1 - 1.5. , SICP.

+1
source

Source: https://habr.com/ru/post/1702575/


All Articles