Thursday, November 13, 2014

Practicing with sets in Advanced Student Language

This is the version I did some time ago using the Advanced Student Language of the sets exercise from the Coursera Scala course:

;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname sets) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ())))
;; Number -> (Number => Boolean)
;; Contains set an elem
(define (contains set elem)
(set elem))
(check-expect
(contains (lambda (x) (= 2 x)) 2)
true)
(check-expect
(contains (lambda (x) (= 2 x)) 3)
false)
;; Number -> (Number => Boolean)
;; Singleton set
(define (singletonSet elem)
(lambda (x)
(= elem x)))
(check-expect
(contains (singletonSet 4) 4)
true)
(check-expect
(contains (singletonSet 3) 4)
false)
;; (Number => Boolean), (Number => Boolean) -> (Number => Boolean)
;; Union of sets
(define (union set1 set2)
(lambda (x)
(or (set1 x)
(set2 x))))
(define set-created-using-union
(union
(lambda (x)
(= (remainder x 2) 0))
(lambda (x) (> x 2))))
(check-expect
(contains set-created-using-union 6)
true)
(check-expect
(contains set-created-using-union 1)
false)
(check-expect
(contains set-created-using-union 7)
true)
;; (Number => Boolean), (Number => Boolean) -> (Number => Boolean)
;; Intersection of sets
(define (intersection set1 set2)
(lambda (x)
(and (set1 x)
(set2 x))))
(define set-created-using-intersection
(intersection
(lambda (x) (< x 4))
(lambda (x) (> x 2))))
(check-expect
(contains
set-created-using-intersection 3)
true)
(check-expect
(contains
set-created-using-intersection 2)
false)
;; (Number => Boolean), (Number => Boolean) -> (Number => Boolean)
;; Difference of sets
(define (diff set1 set2)
(lambda (x)
(and (set1 x)
(not (set2 x)))))
(define set-created-using-diff
(diff (lambda (x) (< x 4))
(lambda (x) (> x 2))))
(check-expect
(contains set-created-using-diff 2)
true)
(check-expect
(contains set-created-using-diff 3)
false)
;; (Number => Boolean), (Number => Boolean) -> Boolean
;; Check if a predicate is true for all members of an interval which is inside [-1000, 1000]
(define (forall? set pred)
(iter (- BOUND) set pred))
(define BOUND 1000)
(define (iter a set pred)
(cond ((> a BOUND)
true)
(((diff set pred) a)
false)
(else
(iter (+ a 1) set pred))))
(check-expect
(forall? (lambda (x) (< -2 x 2))
(lambda (x) (< x 3)))
true)
(check-expect
(forall? (lambda (x) (< -2 x 2))
(lambda (x) (= (remainder x 2) 0)))
false)
;; (Number => Boolean), (Number => Boolean) -> Boolean
;; Check if a predicate is true for any member of an interval which is inside [-1000, 1000]
(define (exists? set pred)
(not (forall? set (diff set pred))))
(check-expect
(exists? (lambda (x) (< -2 x 2))
(lambda (x) (= (remainder x 2) 0)))
true)
(check-expect
(exists? (lambda (x) (< -2 x 2))
(lambda (x) (> x 4)))
false)
;; (Number => Boolean), (Number => Number) -> (Number => Boolean)
;; Maps using a given function a set in an interval which is inside [-1000, 1000]
(define (mapset set func)
(lambda (y) (exists? set (lambda (x) (= (func x) y)))))
(define set-defined-using-mapset
(mapset (lambda (x) (< -3 x 3))
(lambda (x) (* x x))))
(check-expect
(contains set-defined-using-mapset 4)
true)
(check-expect
(contains set-defined-using-mapset 1)
true)
(check-expect
(contains set-defined-using-mapset -2)
false)
(check-expect
(contains set-defined-using-mapset -1)
false)
(define other-set-defined-using-mapset
(mapset (lambda (x) (< -3 x 3))
(lambda (x) (- x x))))
(check-expect
(contains other-set-defined-using-mapset -2)
false)
(check-expect
(contains other-set-defined-using-mapset -1)
false)
(check-expect
(contains other-set-defined-using-mapset 0)
true)
(check-expect
(contains other-set-defined-using-mapset 1)
false)
(check-expect
(contains other-set-defined-using-mapset 2)
false)
view raw sets.rkt hosted with ❤ by GitHub

It helped me to practice with lambdas and to blur the border between data and functions.

No comments:

Post a Comment