From 7b7bf727da3250907284368b98b0083a4b4e2386 Mon Sep 17 00:00:00 2001 From: mryouse Date: Sat, 3 Jun 2023 20:22:18 +0000 Subject: initial commit --- p99/p09.neb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 p99/p09.neb (limited to 'p99/p09.neb') 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))) -- cgit v1.2.3