SICP Exercise 1.3 request for comments

后端 未结 17 1655
耶瑟儿~
耶瑟儿~ 2020-12-08 21:06

I\'m trying to learn scheme via SICP. Exercise 1.3 reads as follow: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two la

17条回答
  •  旧巷少年郎
    2020-12-08 21:25

    You can also sort the list and add the squares of the first and second element of the sorted list:

    (require (lib "list.ss")) ;; I use PLT Scheme
    
    (define (exercise-1-3 a b c)
      (let* [(sorted-list (sort (list a b c) >))
             (x (first sorted-list))
             (y (second sorted-list))]
        (+ (* x x) (* y y))))
    

提交回复
热议问题