-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vd: quick lambda expression/function examples.
- Loading branch information
1 parent
9116bd0
commit 41268ca
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#lang htdp/isl+ | ||
(require 2htdp/image) | ||
|
||
; | ||
; NOTE: isl+ is intermediate student language with lambda | ||
; | ||
|
||
; | ||
; PROBLEM: | ||
; | ||
; Rewite each of the following function definitions using lambda. | ||
; | ||
|
||
;; Number (listof Number) -> (listof Number) | ||
;; produce only elements of lon > threshold | ||
(check-expect (only-bigger 2 empty) empty) | ||
(check-expect (only-bigger 3 (list 2 4 5)) (list 4 5)) | ||
|
||
#; | ||
(define (only-bigger threshold lon) | ||
(local [(define (pred n) | ||
(> n threshold))] | ||
(filter pred lon))) | ||
|
||
(define (only-bigger threshold lon) | ||
(filter (lambda (n) (> n threshold)) lon)) | ||
|
||
;; (listof Image) -> (listof Natural) | ||
;; produce list of areas of images | ||
(check-expect (all-areas (list (rectangle 2 3 "solid" "blue") | ||
(square 10 "solid" "white"))) | ||
(list 6 100)) | ||
|
||
#; | ||
(define (all-areas loi) | ||
(local [(define (area i) | ||
(* (image-width i) | ||
(image-height i)))] | ||
(map area loi))) | ||
|
||
(define (all-areas loi) | ||
(map (lambda (i) | ||
(* (image-width i) | ||
(image-height i))) loi)) | ||
|
||
;; (listof Number) -> (listof Number) | ||
;; produce list of numbers sorted in ASCENDING order | ||
;; ASSUMPTION: lon contains no duplicates | ||
(check-expect (qsort empty) empty) | ||
(check-expect (qsort (list 8)) (list 8)) | ||
(check-expect (qsort (list 7 8 9)) (list 7 8 9)) | ||
(check-expect (qsort (list 4 3 2 1)) (list 1 2 3 4)) | ||
(check-expect (qsort (list 6 8 1 9 3 7 2)) (list 1 2 3 6 7 8 9)) | ||
|
||
#; | ||
(define (qsort lon) | ||
(if (empty? lon) | ||
empty | ||
(local [(define p (first lon)) | ||
(define (<p? n) (< n p)) | ||
(define (>p? n) (> n p))] | ||
(append (qsort (filter <p? lon)) | ||
(list p) | ||
(qsort (filter >p? lon)))))) | ||
|
||
(define (qsort lon) | ||
(if (empty? lon) | ||
empty | ||
(local [(define p (first lon))] | ||
(append (qsort (filter (λ (n) (< n p)) lon)) | ||
(list p) | ||
(qsort (filter (λ (n) (> n p)) lon)))))) | ||
|