“These are you father’s parentheses”
This post is the note for week1 in Programming Language Part-B(Coursera, UW). Racket is a dynamic type language. Lisp, Scheme are its relatives. It also adopts many functional programming concept like high order function, closure, immutable list etc.
Dr. Racket shortcut
- ESC + p : cycle backforward for REPL expression
- ESC + n : cycle forward for REPL expression
Racket 101
Defining a function:
(define (f x y) body)
(define (f x y)
body)
(define f (lambda (x y) body))
; no-argument function:
(define (f) body)
(define f (lambda () body))
Let expressions:
(let ([x1 e1]
[x2 e2]
[f1 (lambda (x) body)])
letBody))
If expressions:
(if testExp
thenExp
elseExp)
Cond expressions:
(cond [test1 exp1]
[test2 exp2]
[test3 exp3]
[#t expDefault])
Section 5
Something I learned in this section:
Immutable List
Dynamic Typing
Delayed Evaluation and Thunk
Stream
Understand the concept of “Thunk” is really important for homework 4
; p4
; stream is a thunk that will produce a pair (first_element, rest_of_the_thunk)
; thunk is a function with zero argument, it's used to delay calling, calling thunk will get the actual value
(define (stream-for-n-steps s n)
(let ([sval (s)]) ; excute s only once
(cond [(= n 0) null]
[(= n 1) (list (car sval))]
[#t (cons (car sval) (stream-for-n-steps (cdr sval) (- n 1)))])))
; p5
; 1, 2, 3, 4, -5, 6, 7 ... -10, 11, ... -15, 16
(define funny-number-stream
(letrec ([f (lambda (x)
(cond [(= (remainder x 5) 0) (cons (- 0 x) (lambda () (f (+ x 1))))]
[#t (cons x (lambda () (f (+ x 1))))]))])
(lambda () (f 1))))
Calling stream will look like :
(stream-for-n-steps funny-number-stream 16)
; we expect to get : (list 1 2 3 4 -5 6 7 8 9 -10 11 12 13 14 -15 16)
Macros
We can use macros to extend syntax to our language. For example, we can define while-less, such that
(define a 2)
(while-less 7 do (begin (set! a (+ a 1)) (print "x") a))
(while-less 7 do (begin (set! a (+ a 1)) (print "x") a))
Evaluating the second line will print “x” 5 times and change a to be 7. So evaluating the third line will print “x” 1 time and change a to be 8.
Here is my solution :
(define-syntax while-less
(syntax-rules (do)
[(while-less e1 do body)
(let ([e e1])
(letrec ([loop (lambda ()
(let ([v body])
(if (>= v e)
#t
(begin v (loop)))))])
(loop)))]))