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