l-99

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Feed

commit 23898ba605062379c3900687c3d2a690fb79c306
Author: Jenny Doe <tng@soykaf.me>
Date:   Sat, 23 Mar 2019 13:29:29 +0100

birthday!

Diffstat:
Ap01.scm | 9+++++++++
Ap02.scm | 9+++++++++
Ap03.scm | 10++++++++++
Ap04.scm | 6++++++
Ap05.scm | 7+++++++
Ap06.scm | 5+++++
Ap07.scm | 18++++++++++++++++++
Ap08.scm | 15+++++++++++++++
8 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/p01.scm b/p01.scm @@ -0,0 +1,9 @@ +; (*) Find the last box of a list. +; Example: +; * (my-last '(a b c d)) +; (D) + +(define (my-last ls) + (if (null? (cdr ls)) ; defined for non-empty lists only + ls + (my-last (cdr ls)))) diff --git a/p02.scm b/p02.scm @@ -0,0 +1,9 @@ +; (*) Find the last but one box of a list. +; Example: +; * (my-but-last '(a b c d)) +; (C D) + +(define (my-but-last ls) + (if (null? (cddr ls)) ; defined when (lenght ls) >= 2 + ls + (my-but-last (cdr ls)))) diff --git a/p03.scm b/p03.scm @@ -0,0 +1,10 @@ +; (*) Find the K'th element of a list. +; The first element in the list is number 1. +; Example: +; * (element-at '(a b c d e) 3) +; C + +(define (element-at ls n) ; (assert (< n (length ls))) + (if (zero? n) + (car ls) + (element-at (cdr ls) (- n 1)))) diff --git a/p04.scm b/p04.scm @@ -0,0 +1,6 @@ +; (*) Find the number of elements of a list. + +(define (my-length ls) + (if (null? ls) + 0 + (+ 1 (my-length (cdr ls))))) diff --git a/p05.scm b/p05.scm @@ -0,0 +1,7 @@ +; (*) Reverse a list. + +(define (my-reverse ls) + (if (null? ls) + '() + (append (my-reverse (cdr ls)) + (list (car ls))))) diff --git a/p06.scm b/p06.scm @@ -0,0 +1,5 @@ +; (*) Find out whether a list is a palindrome. +; A palindrome can be read forward or backward; e.g. (x a m a x). + +(define (is-palyndrome? ls) + (equal? (reverse ls) ls)) diff --git a/p07.scm b/p07.scm @@ -0,0 +1,18 @@ +; (**) Flatten a nested list structure. +; Transform a list, possibly holding lists as elements into a `flat' +; list by replacing each list with its elements (recursively). +; +; Example: +; * (my-flatten '(a (b (c d) e))) +; (A B C D E) +; +; Hint: Use the predefined functions list and append. + +(define (my-flatten ls) + (if (null? ls) + '() + (if (pair? (car ls)) + (append (my-flatten (car ls)) + (my-flatten (cdr ls))) + (cons (car ls) + (my-flatten (cdr ls)))))) diff --git a/p08.scm b/p08.scm @@ -0,0 +1,15 @@ +; (**) Eliminate consecutive duplicates of list elements. +; If a list contains repeated elements they should be replaced with a +; single copy of the element. The order of the elements should not be +; changed. +; +; Example: +; * (compress '(a a a a b c c a a d e e e e)) +; (A B C A D E) + +(define (compress lat) + (if (null? (cdr lat)) + lat + (if (eq? (car lat) (cadr lat)) + (compress (cdr lat)) + (cons (car lat) (compress (cdr lat))))))