diff options
author | Julian T <julian@jtle.dk> | 2021-09-08 12:43:04 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-09-08 12:43:04 +0200 |
commit | 8fe80b1bfe70267b29bb39ccb93f24fdd94847fc (patch) | |
tree | 47e438407dc80dc7e560cb97c3666e97a1910178 /sem7/pp/lec1/exercises.scm | |
parent | c03ad645b32d191dd5aa8c2f492f8067f351d137 (diff) |
Rename to better scheme extension
Diffstat (limited to 'sem7/pp/lec1/exercises.scm')
-rw-r--r-- | sem7/pp/lec1/exercises.scm | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/sem7/pp/lec1/exercises.scm b/sem7/pp/lec1/exercises.scm new file mode 100644 index 0000000..9be35e2 --- /dev/null +++ b/sem7/pp/lec1/exercises.scm @@ -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)) |