diff options
Diffstat (limited to 'p99/p09.neb')
| -rw-r--r-- | p99/p09.neb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/p99/p09.neb b/p99/p09.neb new file mode 100644 index 0000000..619c4a4 --- /dev/null +++ b/p99/p09.neb @@ -0,0 +1,31 @@ +; P09 Pack consecutive duplicates of list elements into sublists. +; If a list contains repeated elements they should be placed in separate sublists. + +(def a (list "a" "a" "a" "a" "b" "c" "c" "a" "a" "d" "e" "e" "e" "e")) + +; the list, the most recent value, the accumulator +(func pack-dup (lst) + (pack-dup lst "" (list))) + +(func pack-dup (lst prev acc) + (branch + ((empty? lst) acc) ; if we're done, return acc + ((eq? (first lst) prev) ; the item is the same as the previous item + (pack-dup + (rest lst) + prev + (append ; add item to the last list in acc + (most acc) + (append + (last acc) + prev)))) + (#true + (pack-dup + (rest lst) + (first lst) + (append + acc + (list (first lst))))))) ; add new list with this item + +(print (->string a)) +(print (->string (pack-dup a))) |
