Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Programs by Jay and Matthew at Lambda Lounge Utah, 14 Jan 2014: * "functional.rkt" is from Jay's introduction to functional programming * "web.rkt" and "page.rkt" are from Matthew's explanation of Racket macros & modules

View functional.rkt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#lang racket
(require rackunit)
 
;; A singly linked list is either
;; - NULL
;; - pointer to data and a pointer a sll
(struct node (data-ptr next-ptr))
 
(define n4 (node 4 null))
 
;; n4->data-ptr
(check-equal? (node-data-ptr n4) 4)
 
(define n54 (node 5 n4))
 
;; n54->next-ptr->data-ptr
(check-equal? (node-data-ptr (node-next-ptr n54)) 4)
 
;; A list is either
;; - null
;; - (cons data list)
 
(define c4 (cons 4 null))
(define c54 (cons 5 c4))
 
(check-equal? (car (cdr c54)) 4)
 
;; c[ad]+r
 
(check-equal? (cadr c54) 4)
 
(check-equal? (first (rest c54)) 4)
(check-equal? (second c54) 4)
 
;; length : list(a) -> num
;; (length l) = 42
(define (length l)
(if (cons? l)
(+ 1 (length (rest l)))
0))
 
;; f(x) = x + 4
;; f(5) = 5 + 4
 
(check-equal? (length c54) 2)
(check-equal? (length null) 0)
(check-equal? (length c4) 1)
 
;; expression problem
;; fun = easy to add functions
;; oo = easy to add kinds
 
;; all-even? : list num -> bool
(define (all-even? l)
(if (empty? l)
true
(and (even? (first l))
(all-even? (rest l)))))
 
(check-equal? (all-even? null) true)
(check-equal? (all-even? (list 1 2 3)) false)
(check-equal? (all-even? (list 2 4 6)) true)
 
;; higher-order function
 
(check-equal? (all-even? (list 2 4 6))
(all-even? (cons 2 (list 4 6))))
(check-equal? (all-even? (cons 2 (list 4 6)))
(and (even? (first (cons 2 (list 4 6))))
(all-even? (rest (cons 2 (list 4 6))))))
(check-equal? (all-even? (cons 2 (list 4 6)))
(and (even? 2)
(all-even? (list 4 6))))
(check-equal? (all-even?
(cons 2
(cons 4
(cons 6
empty))))
(and (even? 2)
(and (even? 4)
(and (even? 6)
true))))
;; all-even?s job is to...
;; turn empty into true
;; turn (cons a r) into (and (even? a) r)
;; greek name for this: catamorphism
 
(define (all-even?/awesome l)
(foldr (lambda (a d)
(and (even? a) d))
true
l))
 
(check-equal? (all-even?/awesome null) true)
(check-equal? (all-even?/awesome (list 1 2 3)) false)
(check-equal? (all-even?/awesome (list 2 4 6)) true)
 
;; foldr : (A B -> B) B (list A) -> B
 
;; int f ( int x, int y ) { ... }
;; int int -> int
 
(define (sum l)
;; + : (A B -> B) : (nat nat -> nat)
;; 0 : B : nat
;; l : (list A) : (list nat)
(foldr + 0 l))
 
(define arithmetic (list + - * /))
 
(check-equal? (sum (list 1 2 3 4)) 10)
 
;; map : (A -> B) (list A) -> (list B)
 
(define (evenify l)
(map even? l))
 
(check-equal? (evenify (list 1 2 3 4))
(list false true false true))
 
;; A bt is either a
(struct bt-leaf () #:transparent)
(struct bt-node (left val right) #:transparent)
 
;; these are all (bt num)
(define b5 (bt-node (bt-leaf) 5 (bt-leaf)))
(define b6 (bt-node b5 6 (bt-leaf)))
(define bb (bt-node (bt-node (bt-leaf) 3 (bt-leaf))
4
b6))
 
;; lookup : (bt A) A -> bool
(define (lookup bt v)
(cond
[(bt-leaf? bt)
false]
[else
(cond
[(= v (bt-node-val bt))
true]
[(< v (bt-node-val bt))
(lookup (bt-node-left bt) v)]
[else
(lookup (bt-node-right bt) v)])]))
 
(check-equal? (lookup bb 5) true)
(check-equal? (lookup bb 2) false)
 
;; insert : (bt A) A -> (bt A)
(define (insert bt v)
(cond
[(bt-leaf? bt)
(bt-node (bt-leaf) v (bt-leaf))]
[else
(cond
[(= v (bt-node-val bt))
bt]
[(< v (bt-node-val bt))
(bt-node (insert (bt-node-left bt) v)
(bt-node-val bt)
(bt-node-right bt))]
[else
(bt-node
(bt-node-left bt)
(bt-node-val bt)
(insert (bt-node-right bt) v))])]))
 
;; lg n in time
;; lg n in space
 
(check-equal? (lookup bb 2) false)
(check-equal? (lookup (insert bb 2) 2) true)
(check-equal? (lookup bb 2) false)
 
(struct zipper (path focus))
(struct path-tree-left (val right))
(struct path-tree-right (left val))
 
(define bbz (zipper empty bb))
 
(define (move-left z)
(match-define (zipper path focus) z)
(match focus
[(bt-leaf)
(error 'move-left "Can't")]
[(bt-node left val right)
(zipper (cons (path-tree-left val right)
path)
left)]))
 
(define (move-right z)
(match-define (zipper path focus) z)
(match focus
[(bt-leaf)
(error 'move-left "Can't")]
[(bt-node left val right)
(zipper (cons (path-tree-right left val)
path)
right)]))
 
(define (move-up z)
(match-define (zipper path focus) z)
(match path
[(list)
(error 'move-up "Can't")]
[(cons (path-tree-left val right) old-path)
(zipper old-path (bt-node focus val right))]
[(cons (path-tree-right left val) old-path)
(zipper old-path (bt-node left val focus))]))
 
(define (replace z v)
(match-define (zipper path focus) z)
(zipper path v))
 
(zipper-focus
(move-up
(move-up
(replace (move-right (move-left bbz))
(bt-node (bt-leaf) 3.5 (bt-leaf))))))
View functional.rkt
1 2 3 4 5 6 7 8
#lang s-exp "web.rkt"
 
(div ([style "color: blue"])
(a ([href "http://racket-lang.org"]
[style "font-weight: bold"])
"Hello "
"world")
" bye")
View functional.rkt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#lang racket
(require web-server/servlet-env
web-server/http/xexpr
(for-syntax syntax/parse))
 
(provide (except-out (all-from-out racket)
#%module-begin)
(rename-out [module-begin #%module-begin]))
 
(define-syntax-rule (module-begin expr)
(#%module-begin (page expr)))
 
(define (show v)
(print v)
(newline)
v)
 
(define-syntax define-tag
(lambda (stx)
(syntax-parse stx
[(define-tag tag ok-attrib ...)
#'(begin
(define-syntax tag
(lambda (stx)
(syntax-parse stx
[(tag ([attrib s-expr] (... ...)) content-expr (... ...))
(unless (ormap (lambda (a) (member (syntax-e a)
'(ok-attrib ...)))
(syntax-e #'(attrib (... ...))))
(raise-syntax-error #f "bad attribute" stx))
#'(show
`(tag ([attrib ,s-expr] (... ...))
,content-expr (... ...)))])))
(provide tag))])))
 
(define-tag div style)
(define-tag a href style)
 
(define (page content)
(serve/servlet
(lambda (req)
(response/xexpr
content))))
 
(provide page)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.