Code Examples
Code Examples
(define (myrev l)
(if (null? l)
'()
(append (myrev (cdr l)) (list (car l)))
)
)
---------------------------------------------------------------------
> (define qux (list 1 2 3 4))
> (define ((define (my-foldr fn start lst)
(display lst)
(newline)
(if (null? lst)
start
(fn (car lst) (my-foldr fn start (cdr lst)))))
> (my-foldr * 1 qux)
-------------------------------------------------------------
(define (my-list-ref lst n)
(if (zero? n)
(car lst)
(my-lst-ref (cdr lst) (- n 1))))
---------------------------------------------------------------------
(define (take-while p l)
(let loop ((o '()) (l l))
(if (and (not (null? l)) (p (car l)))
(loop (cons (car l) o) (cdr l))
(reverse o))))
(define (up-to-first-digit l)
(take-while (lambda (x) (not (number? x))) l))
(define (min a b)
(if (< a b)
a
b))
------------------------------------------------------------------
HW 2
Problem #4
(define sub-num
(lambda (list)
(cond ((null? list) null)
(else (cons (- (car list) 2)
(sub-num (cdr list)))))))
--------------
> (sub-num '(7 20 9))
(5 18 7)
> (sub-num '())
()
>
------------------------------------------------------------------
Problem #5
(define (add-num ls)
(cond ((null? ls) 0)
(else (+ (car ls) (add-num (cdr ls))))
)
)
> (add-num (filter number?'(2 3 r 5 e 9 7)))
-------------------------------------------------------------------
Problem #6
Notes
---------------------------------------------------------
(define (sum_even l)
(if (null? l) l
(cond ((even? (car l)) 0)
((not(even? (car l))) (car l)))
(+ (sum_even (car l) (sum_even(cdr l))))))
(sum_even '(2 3 4))
#1
(define my-list '(1 a b c 4 5 2))
(define new-list '(0))
my-list
(define (list-ref2 lst n)
(cond
((empty? lst) #f)
((= n 0) (car lst))
(else (list-ref2 (cdr lst) (- n 1)))))
(1 a b c 4 5 2)
> (sum my-list new-list)
#f
>
#2
(define my-list '(1 a b c 4 5 2))
(define new-list '(0 0 0 0 0 0 0))
(define new-list '(0))
my-list
(define (list-ref2 lst n)
(cond
((empty? lst) #f)
((= n 0) (car lst))
(else (list-ref2 (cdr lst) (- n 1)))))
(add-num my-list)
HW 3 scheme problems
Problem # 4
(define turn
(lambda (f null xs)
(if (null? xs)
null
(turn f (f (car xs) null) (cdr xs)))))
(define reverseit
(lambda (xs)
(turn cons '() xs)))
-----------
> (reverseit '(1 2 3 4))
(4 3 2 1)
> (reverseit '(1 2 (3 4) 5))
(5 (3 4) 2 1)
>
---------------------------------------------------------------------------
Problem #5
HW 4 scheme problems
Problem #6
HW 4 written problems
Problem #4
∧ /\ (slash, backslash)
∨ \/ (backslash, slash)
∀ forall
∃ exists
¬−
Problem #3
Code Examples
(define (myrev l)
(if (null? l)
'()
---------------------------------------------------------------------
(display lst)
(newline)
start
-------------------------------------------------------------
(if (zero? n)
(car lst)
---------------------------------------------------------------------
(define (take-while p l)
(reverse o))))
(define (up-to-first-digit l)
-------------------------------------------------------------------
(define (first-syms x)
'()))
---------------------------------------------------------------
(car list-of-values)
(+ (car list-of-values)
----------------------------------------------------------------
if (a < b)
return a;
else
return b;
}
(define (min a b)
(if (< a b)
b))
Problem #6
Notes
---------------------------------------------------------
(define (sum_even l)
(if (null? l) l
Problem #2 HW 2
(cond
21+15+10 = 46 45.54%
20+17+5+10+10 = 62 62%
HW1 90 10%
5+5+2+19+5+10+10+10 = 66 73.33%
\
#1
my-list
(cond
((number? (car my-list)) (and (= (car new-list) (+ (car my-list) (car new-list))) (sum (cdr my-list) new-
list)))
-------------------------------
(1 a b c 4 5 2)
#f
>