aboutsummaryrefslogtreecommitdiff
path: root/sem7
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-09-08 12:34:43 +0200
committerJulian T <julian@jtle.dk>2021-09-08 12:35:37 +0200
commitc03ad645b32d191dd5aa8c2f492f8067f351d137 (patch)
tree428a9f3fd722ca29aceecd4dea4478305a17a56a /sem7
parent9e828fa04231d2d01c5c7701375484a3beea79ed (diff)
Add some exercises for scheme course
Diffstat (limited to 'sem7')
-rw-r--r--sem7/pp/lec1/exercises.sch36
1 files changed, 36 insertions, 0 deletions
diff --git a/sem7/pp/lec1/exercises.sch b/sem7/pp/lec1/exercises.sch
new file mode 100644
index 0000000..9be35e2
--- /dev/null
+++ b/sem7/pp/lec1/exercises.sch
@@ -0,0 +1,36 @@
+(define (make-list-ft from to)
+ (if (> from to)
+ '()
+ (cons from (make-list-ft (1+ from) to))))
+
+;; Exercise 1.3 (proper lists)
+;;? Program own version of list?, thus proper-list?
+;; This is done by running to the end of list, always checking if pair.
+;; If last element is '() its a proper list, otherwize its not.
+(define (proper-list? list)
+ (if (pair? list)
+ (proper-list? (cdr list))
+ (null? list)))
+
+;;? Can you write your predicate without using if or cond?
+;; Hmm i cant see how this would be done, as we are using recursion which requires a stop condition.
+
+;;? Is your function efficient? What is the time complexity?
+;; This runs over all the elements in the list, so O(n).
+;; Hmm reading through the solution, i see that we can use other conditional functions like `or` and `and`.
+;; Lets try that
+(define (proper-list? list)
+ (or (null? list) (and (pair? list) (proper-list? (cdr list)))))
+
+;; Okay thats just his solution
+
+;; Exercise 1.5 (every second element)
+;; Hmm the text mentions that we could write it with (every-nth-element)
+(define (every-nth-element n list)
+ (let recur ((list list) (index 0))
+ (cond ((null? list) '())
+ ((zero? (modulo index n)) (cons (car list) (recur (cdr list) (1+ index))))
+ (else (recur (cdr list) (1+ index))))))
+
+(define (every-2th-element list)
+ (every-nth-element 2 list))