0% found this document useful (0 votes)
9 views

Code Examples

Uploaded by

Mack Wenberg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Code Examples

Uploaded by

Mack Wenberg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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))

(up-to-first-digit '(q w c 9 5 6)) ;=> (q w c)


-------------------------------------------------------------------
(define (first-syms x)
(if (and (pair? x) (symbol? (car x)))
(cons (car x) (first-syms (cdr x)))
'()))
---------------------------------------------------------------
(define (sum list-of-values)
(if (= 1 (length list-of-values))
(car list-of-values)
(+ (car list-of-values)
(sum (cdr list-of-values)))))
----------------------------------------------------------------
int min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
In Scheme, we can just do this:

(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)))))

(define (sum my-list new-list)


(cond ((char? (car my-list)) (sum (cdr my-list) new-list))
((number? (car my-list)) (and (= (car new-list) (+ (car my-list) (car new-list))) (sum (cdr
my-list) new-list)))
((= 0 (length my-list)) read (new-list))))

(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)))))

(define (add-num my-list)


(cond ((char? (car my-list)) add-num (cdr my-list))
((number? (car my-list)) (= (car new-list) (+ (car my-list) (car new-list))))
((> 0 (length my-list)) add-num (cdr my-list))
(new-list)))

(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

(define (deepreverse list)


(define (deepreverseB list acc)
(if (null? list)
acc
(if (list? (car list))
(deepreverseB (cdr list) (cons (deepreverse (car list)) acc))
(deepreverseB (cdr list) (cons (car list) acc)))))
(deepreverseB list '()))
-------------
> (deepreverse '(1 (2 3) 4))
(4 (3 2) 1)
> (deepreverse '((1 2) (3 4)))
((4 3) (2 1))
>
------------------------------------------------------------------------------
Problem #6

(define (sum-all lst)


(cond ([empty? lst] 0)
([list? (car lst)] (+ (sum-all (car lst)) (sum-all (cdr lst))))
(else (+ (car lst)(sum-all (cdr lst))))))
-----------------
> (sum-all '((1 3) (5 7) (9 11)))
36
> (sum-all '(1 (3 (5 (7 (9))))))
25
> (sum-all '())
0
>

HW 4 scheme problems

Problem #6

(define (count-occur x list)


(if (null? list)
0
(if (eqv? (car list) x)
(+ 1 (count-occur x (cdr list)))
(count-occur x (cdr list)))))
-----------
> (count-occur 'a '(a a a b b b b b b))
3
>

HW 4 written problems
Problem #4

∧ /\ (slash, backslash)

∨ \/ (backslash, slash)

∀ forall

∃ exists
¬−

⇒ => (equals, greater than)

P1. Mythical ⇒ Immortal

P2. ¬Mythical ⇒ ¬Immortal ∧ M ammal

P3. Immortal ∨ M ammal ⇒ Horned

P4. Horned ⇒ M agical

1. ¬Immortal ⇒ ¬Mythical P1 contrapositive

2. ¬Immortal ⇒ ¬Immortal ∧ M ammal 1, P2, hypothetical syllogism

3. Immortal ∨ (¬Immortal ∧ M ammal) 2, implication defn

4. (Immortal ∨ ¬Immortal) ∧ (Immortal ∨ M ammal) 3, distributive

5. Immortal ∨ M ammal 4, tautology, identity


6. Horned 5, P3, modus ponens
7. Mythical 6, P4, modus ponens
So the unicorn is horned and mythical. However, there is no way to show the unicorn is
mythical.
----------------------------------------------------

Problem #3

¬Smoke v Smoke implication elimination


TRUE definition of v
Valid

|Smoke |Smoke → Smoke| (table)


0 1
1 1

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))

(up-to-first-digit '(q w c 9 5 6)) ;=> (q w c)

-------------------------------------------------------------------

(define (first-syms x)

(if (and (pair? x) (symbol? (car x)))

(cons (car x) (first-syms (cdr x)))

'()))

---------------------------------------------------------------

(define (sum list-of-values)

(if (= 1 (length list-of-values))

(car list-of-values)

(+ (car list-of-values)

(sum (cdr list-of-values)))))

----------------------------------------------------------------

int min(int a, int b)

if (a < b)

return a;

else

return b;
}

In Scheme, we can just do this:

(define (min a b)

(if (< a b)

b))

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))

Problem #2 HW 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)))))]

56.89% in the class

HW4 101 12%

21+15+10 = 46  45.54%

HW3 110 10%

20+10+10+10+5 = 55  50% (+25  72%)((10 + 10) 

HW2 100 10%

20+17+5+10+10 = 62  62%

HW1 90 10%

5+5+2+19+5+10+10+10 = 66  73.33%

Total: 101+110+100+90 = 401  60%

Projected: 46+55+62+66 = 229  57.10%

\
#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)))))

(define (sum my-list new-list)

(cond ((char? (car my-list)) (sum (cdr my-list) new-list))

((number? (car my-list)) (and (= (car new-list) (+ (car my-list) (car new-list))) (sum (cdr my-list) new-
list)))

((= 0 (length my-list)) read (new-list))))

-------------------------------

(1 a b c 4 5 2)

> (sum my-list new-list)

#f

>

7.92 % HW4  12%

42.00 % HW2  10%

0.00 % HW3  10%

73.33 % HW1  10%

You might also like