From 8fe80b1bfe70267b29bb39ccb93f24fdd94847fc Mon Sep 17 00:00:00 2001 From: Julian T Date: Wed, 8 Sep 2021 12:43:04 +0200 Subject: Rename to better scheme extension --- sem7/pp/lec1/exercises.scm | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 sem7/pp/lec1/exercises.scm (limited to 'sem7/pp/lec1/exercises.scm') 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)) -- cgit v1.2.3