;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname more_lambda) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp"))))) (build-list 10 (lambda (x) (* x x))) ;; if I want it starting at 1? ;; modifying the lambda function (check-expect (build-list 10 (lambda (x) (* (+ x 1) (+ x 1)))) (list 1 4 9 16 25 36 49 64 81 100)) ;; using a trick: generate 11 elements starting at 0, ;; and take the last 10 (check-expect (rest (build-list 11 (lambda (x) (* x x)))) (list 1 4 9 16 25 36 49 64 81 100)) ;; if I want to define a function that, given n, returns the ;; first n squares, starting at 0? (define (squares n) (local ((define (sq x) (* x x))) (build-list n sq)) ) (squares 5) ;; using lambda: (define (squares1 n) (build-list n (lambda (x) (* x x))) ) (squares1 5) ;; write a function build-range that takes two integers, ;; n and m, n <=m, and returns a list (n n+1 ... m) (define (build-range n m) (build-list (+ (- m n) 1) (lambda (x) ( + x n)))) ;; Test cases: (check-expect (build-range 2 2) (list 2)) (check-expect (build-range 2 5) (list 2 3 4 5)) ;; use lambda to define the following functions: ;; a function that takes two numbers and returns the sum of their squares (define sum-squares (lambda (x y) (+ (* x x) (* y y)))) (check-expect (sum-squares 2 3) 13) ;; a function that takes two integers and returns true if the first number ;; is divisible by the second one. ;; hint: use modulo function (define divisible? (lambda (n m) (= (modulo n m) 0))) (check-expect (divisible? 4 2) true) (check-expect (divisible? 5 2) false)