; 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)))