/avatar.jpg

Lec25-Data Examples

Data Examples Lists in Environment Diagrams 注意:切片 or addition 是引用,而不是复制,有时候可能不经意改变 1 2 3 4 5 >>> t = [1,2,3] >>> t[1:3] = [t] >>> t.extend(t) >>> print(t) [1, [...], 1, [...]] 在Python中,当你尝试打印一个包含自身的列表时,列表无法递归地打印出所有的元素,因为它会无限循环地引用自己。为了避免这种无限循环,Python会用[…]来表示列表中被省略的部分。 Obj Systems in Python return a string -> 'str', print a string -> str .some_attr: can create a new attribute for the instance at once, or get the value of an existing attribute Iterators and Iterables in Python training test

Lec26-Scheme

Scheme Scheme Fundamentals 1 2 3 (number? 123) ; #t (number? "123") ; #f (string? "hello") ; #t Special Forms 1 2 3 4 5 6 7 8 9 10 11 (define x 10) ; define a variable x with value 10 (if #t 10 20) ; if #t is true, return 10, otherwise return 20 (cond ((= x 10) "x is 10") ((= x 20) "x is 20") (else "x is not 10 or 20")) ; conditional statement (and #t #f) ; #f (or #t #f) ; #t (let ((x 10) (y 20)) (+ x y)) ; let statement to create local variables (let* ((x 10) (y (+ x 10))) (+ x y)) ; let* statement to create local variables and use the value of previous variables (lambda (x) (+ x 10)) ; lambda expression to create a function that adds 10 to a given number (map (lambda (x) (+ x 10)) (list 1 2 3)) ; map function to apply a function to each element of a list (define (sum-of-squares x y) (+ (* x x) (* y y))) ; define a function that takes two numbers and returns their sum of squares (sum-of-squares 3 4) ; 25 lambda expression 1 2 (define (add-ten x) (+ x 10)) ; define a function that adds 10 to a given number (add-ten 5) ; 15 Cond and Begin let 临时绑定