asb: head /dev/brain > /dev/www

My home, musings, and wanderings on the world wide web.

99 LISP problems: Problem #13

Solution to the 99 LISP Problems #13

I didn’t enjoy this problem very much. The solution is very similar to that for #9. But sticks to the requirements of the problem (count directly without making sublists).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(defun count-first (alist)
  (if (null alist) 0
    (if (equalp (car alist) (cadr alist))
      (1+ (count-first (cdr alist))) 1)))

(defun trim-first (alist)
  (if (null alist)
    nil
    (if (not (equalp
               (car alist)
               (cadr alist)))
      (cdr alist)
      (trim-first (cdr alist)))))

(defun encode-direct (alist)
  (if (null alist)
    nil
    (append
      (if (= (count-first alist) 1)
        (list (car alist))
        (list (list (count-first alist) (car alist))))
      (encode-direct (trim-first alist)))))

Lisp dialect: Steel Bank Common Lisp

99 LISP problems: Problem #12

Solution to the 99 LISP Problems #12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(defun decompress-one (n elem)
  (if (<= n 0)
    nil
    (append
      (list elem)
      (decompress-one (1- n) elem))))

(defun decode-rle (encoding)
  (if (null encoding)
    nil
    (append
      (if (listp (car encoding))
        (decompress-one (caar encoding) (second (car encoding)))
        (list (car encoding)))
      (decode-rle (cdr encoding)))))

Lisp dialect: Steel Bank Common Lisp

99 LISP problems: Problem #11

Solution to the 99 LISP Problems #11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(defun pack-first (alist)
  (if (null alist)
    nil
    (if (equalp
          (car alist)
          (cadr alist))
      (append (list (car alist)) (pack-first (cdr alist)))
      (list (car alist)))))

(defun trim-first (alist)
  (if (null alist)
    nil
    (if (not (equalp
               (car alist)
               (cadr alist)))
      (cdr alist)
      (trim-first (cdr alist)))))

(defun encode-modified (alist)
  (if (null alist)
    nil
    (append
      (if (= (length (pack-first alist)) 1)
        (list (car alist))
        (list (list (length (pack-first alist)) (car alist))))
      (encode-modified (trim-first alist)))))

Lisp dialect: Steel Bank Common Lisp

99 LISP problems: Problem #10

Solution to the 99 LISP Problems #10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(defun pack-first (alist)
  (if (null alist)
    nil
    (if (equalp
          (car alist)
          (cadr alist))
      (append (list (car alist)) (pack-first (cdr alist)))
      (list (car alist)))))

(defun trim-first (alist)
  (if (null alist)
    nil
    (if (not (equalp
               (car alist)
               (cadr alist)))
      (cdr alist)
      (trim-first (cdr alist)))))

(defun encode (alist)
  (if (null alist)
    nil
    (append
      (list (list (length (pack-first alist)) (car alist)))
      (encode (trim-first alist)))))

Lisp dialect: Steel Bank Common Lisp

99 LISP problems: Problem #9

Solution to the 99 LISP Problems #9

This was the first challenging problem I encountered in this set. After spending the first few minutes, I started worrying that this problem may not fit into a simple recursive solution. I did not want to write a do or for loop etc.

What I was looking for was some ingenuously simple recursive solution to this problem (without the use of a helper or a lambda). After quite a bit of struggle (and some looking around) I realized there isn’t one (that I could find, at least). Therefore, I implemented this as a recursive solution by implementing two new verbs (themselves recursive) specifically for this problem.