-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution1.rkt
More file actions
112 lines (86 loc) · 2.55 KB
/
solution1.rkt
File metadata and controls
112 lines (86 loc) · 2.55 KB
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
#lang racket
;; homework 1
; exercise 1
(define (gcd a b)
(if (zero? b) a
(gcd b (modulo a b))))
; exercise 2
(define (nested-length lst)
(if (null? lst)
0
(if (list? (car lst))
(+ (nested-length (car lst)) (nested-length (cdr lst)))
(+ 1 (nested-length (cdr lst))))))
(define (nested-length2 lst)
(define (aux elem acc)
(if (list? elem) (+ acc (foldl aux 0 elem)) (+ acc 1)))
(foldl aux 0 lst))
; ex 3
(define (print-matrix v)
(let row-loop ((i 0))
(unless (= i (vector-length v))
(let ((row (vector-ref v i)))
(if (vector? row)
(let col-loop ((j 0))
(unless (= j (vector-length row))
(display (vector-ref row j))
(display "\t")
(col-loop (+ j 1))))
(display row))
(newline)
(row-loop (+ i 1))))))
; ex 4
(define (vector*! v1 v2 v3)
(let loop ((i 0))
(unless (= i (vector-length v2))
(vector-set! v1 i (make-vector (vector-length v3)))
(let ((n (vector-ref v2 i)) (v (vector-ref v1 i)))
(let mul ((j 0))
(unless (= j (vector-length v3))
(vector-set! v j (* n (vector-ref v3 j)))
(mul (+ j 1)))))
(loop (+ i 1)))))
(define v1 (make-vector 2))
(vector*! v1 #(3 2) #(4 5 7))
; ex 5
(define (zip l1 l2)
(if (or (null? l1) (null? l2))
'()
(cons (list (car l1) (car l2))
(zip (cdr l1) (cdr l2)))))
(define zipped (zip '(1 2 3) '(4 5 6)))
; ex 6
(define (split lst splitter)
(define (split-aux lst acc res)
(cond
((null? lst) (if (null? acc) res (append res (list acc))))
((equal? (car lst) splitter)
(split-aux (cdr lst) '() (append res (if (null? acc) acc (list acc)))))
(else (split-aux (cdr lst) (append acc (list (car lst))) res))))
(split-aux lst '() '()))
(split '(0 1 0 1 0 1 0 2 0 0 4 0) 0)
; ex 7
(define (takewhile pred lst)
(let ((elem (car lst)))
(cond
((null? lst) '())
((pred elem) (cons elem (takewhile pred (cdr lst))))
(else '()))))
#! (takewhile negative? (sequence -5 10))
; ex 8
(define (flatmap f lst)
(if (null? lst)
'()
(let ((elem (car lst)))
(if (list? elem)
(append (flatmap f elem) (flatmap f (cdr lst)))
(cons (f elem) (flatmap f (cdr lst)))))))
; ex 9
(define-syntax on-sign
(syntax-rules (zero: neg: pos:)
((on-sign val pos: pcode zero: zcode neg: ncode)
(let ((v val))
(cond
((positive? v) pcode)
((zero? v) zcode)
(else ncode))))))