diff options
Diffstat (limited to 'p99/p13.neb')
| -rw-r--r-- | p99/p13.neb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/p99/p13.neb b/p99/p13.neb new file mode 100644 index 0000000..e32d8cb --- /dev/null +++ b/p99/p13.neb @@ -0,0 +1,30 @@ +; P13 Run-length encoding of a list (direct solution). +; Implement the so-called run-length encoding data compression method directly. +; I.e. don't explicitly create the sublists containing the duplicates, as in problem P09, +; but only count them. As in problem P11, simplify the result list by replacing the +; singleton lists (1 X) by X. + +(def a (list "a" "a" "a" "a" "b" "c" "c" "a" "a" "d" "e" "e" "e" "e")) + +(func encode-direct (lst) + (encode-direct lst "" (list))) + +(func encode-direct (lst :nil prev acc) + acc) + +(func encode-direct (lst :{:any} prev acc) + (if (eq? (first lst) prev) + (encode-direct + (rest lst) + prev + (append (most acc) + (if (list? (last acc)) + (list (+ 1 (first (last acc))) prev) + (list 2 prev)))) + (encode-direct + (rest lst) + (first lst) + (append acc (first lst))))) + +(print (->string a)) +(print (->string (encode-direct a))) |
