#lang racket ;; ----------------------------------------------------------------- ;; Questions about comments ;; ----------------------------------------------------------------- ; a single-line comment ;; a single-line comment #| a multi-line comment (fifth '(a b c d e)) (list-ref '(a b c d e) 3) (eq? 'x '(x)) that ends on the next line |# ;; ----------------------------------------------------------------- ;; (* 2 pi 1) ; for a circle with radius = 1 ;; (* 2 pi 10) ; for a circle with radius = 10 ;; (* 2 pi 14.1) ; for a circle with radius = 14.1 ;; def circumference(radius): ;; return 2 * pi * radius ;; (lambda (radius) ;; (* 2 pi radius)) (define (circumference radius) (* 2 pi radius)) ;; (define circumference ;; (lambda (radius) ;; (* 2 pi radius))) ;; ----------------------------------------------------------------- (define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) ;; ----------------------------------------------------------------- (define list-of-students '((jerry 3.7 4.0 3.3 3.3 3.0 4.0) (elaine 4.0 3.7 3.7 3.0 3.3 3.7) (george 3.3 3.3 3.3 3.3 3.7 1.0) (cosmo 2.0 2.0 2.3 3.7 2.0 4.0))) ;; (average 3.7 4.0 3.3 3.3 3.0 4.0) (define (compute-gpa student) (apply average (rest student))) (define (sum-of-squares* list-of-numbers) ;; takes a list, not just two (apply + (map square list-of-numbers))) ;; ----------------------------------------------------------------- (define (love-animals animal1 animal2) (string-append "I love baby " animal1 " and " animal2 ".")) (define (love-favorite fav) (lambda (animal2) (love-animals fav animal2))) ;; ----------------------------------------------------------------- ;; This is a helper function I used in the compute-gpa example. ;; We will learn how to write procedures like average next time. ;; ----------------------------------------------------------------- (define (average . grades) (/ (apply + grades) (length grades))) ;; -----------------------------------------------------------------