Three More List Manipulation Exercises
October 14, 2016
Once again, Scheme makes these exercises simple. Here’s a sample list:
(define xs '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20))
We give two solutions to the first task; both use the (make-list n x)
function from the Standard Prelude:
(define (task1a xs ns) (reverse (apply append (map make-list ns xs)))) (define (task1b xs ns) (reverse (mappend make-list ns xs))) > (task1a '(a b c) '(1 2 3)) (c c c b b a) > (task1b '(a b c) '(1 2 3)) (c c c b b a) > (task1a '(d c b a) '(3 0 0 1)) (a d d d) > (task1b '(d c b a) '(3 0 0 1)) (a d d d)
The second of those two functions uses mappend
from the Standard Prelude, which is like map
except that it uses append
instead of cons
. You should get familiar with functions like mappend
, because code you write with them is more concise and easier to read, and because it makes you think of the patterns in your coding. We also have two versions of the second task:
(define (task2a xs) (let loop ((xs xs) (odds (list)) (evens (list))) (cond ((null? xs) (append (reverse odds) (reverse evens))) ((odd? (car xs)) (loop (cdr xs) (cons (car xs) odds) evens)) (else (loop (cdr xs) odds (cons (car xs) evens)))))) (define (task2b xs) (append (filter odd? xs) (filter even? xs))) > (task2a xs) (1 3 5 7 9 11 13 15 17 19 2 4 6 8 10 12 14 16 18 20) > (task2b xs) (1 3 5 7 9 11 13 15 17 19 2 4 6 8 10 12 14 16 18 20)
Again, using a suitable function, like filter
, makes the code clear and concise. There is no loss of efficiency in using filter
, and no reason to prefer the version using a loop. Again, we have two versions of the third function:
(define (task3a xs n) (let ((len (length xs))) (list-ref xs (- len n 1)))) (define (task3b xs n) (list-ref (reverse xs) n)) > (task3a xs 5) 15 > (task3b xs 5) 15
To my mind, the second version is simpler because it doesn’t require the auxiliary variable len
, or the arithmetic of computing the index. You can run the program at http://ideone.com/iYhPhH, where you will also see mappend
and make-list
.
Here’s a solution in Java.
Output:
My solution in clojure:
Output:
(t1 ‘(2 3 4 1) ‘(4 5 6 1)) => (1 1 1 1 4 4 4 4 4 3 3 3 3 3 3 2)
(t2 ‘(1 2 3 4 5 6 7 8)) => (1 3 5 7 2 4 6 8)
(t3 ‘(1 2 3 4 5 5 6 7) 3) => 5
Reblogged this on Computer geek.
@miguelpinia, it looks like we had different interpretations of task 1. I associated “reverse order” with the returned list. It looks like you associated “reverse order” with the input list.
Here’s the Clojure version of my interpretation, based on your code.
@V, are your functions intended to operate on Ruby Arrays? My interpretation of the problem was that ‘list’ refers to ‘linked list’ (based on the comment, “… practice with linked lists”), but after reading again I see that the tasks all say ‘list’, not ‘linked list’.
@miguelpinia There are no build-in linked lists in Ruby, so my implementation is intended for Arrays. I thought for this simple exercise LinkedList, List or Array could be used interchangeably.
@Daniel: It does mention “practice with linked lists” in the first line. Makes a nice exercise in pointer manipulation. Here’s C++ solutions to a couple of the previous set of exercises and the odd/even rearrangement problem: