Posts

Showing posts from August, 2011

Compress-a-sequence

During my attempt to learn LISP/Clojure, I tried the problems in www.4Clojure.com 4Clojure problem #30 : Write a function which removes consecutive duplicates from a sequence. so that ( = ( __ [ 1 1 2 3 3 2 2 3 ] ) '( 1 2 3 2 3 ) ) and ( = ( __ [ [ 1 2 ] [ 1 2 ] [ 3 4 ] [ 1 2 ] ] ) '( [ 1 2 ] [ 3 4 ] [ 1 2 ] ) ) My solution : ;; ywidyatama's solution to Compress a Sequence ;; https://4clojure.com/problem/30 ( fn dedupstr2 [ s ] ( reduce ( fn dedup-reduce [ seq_or_n1 n2 ] ( let [ lastn1 ( if ( seq? seq_or_n1 ) ( last seq_or_n1 ) seq_or_n1 ) , seq1 ( if ( seq? seq_or_n1 ) seq_or_n1 ( seq [ seq_or_n1 ])) ] ( if ( = n2 lastn1 ) seq1 ( concat seq1 ( seq [ n2 ]))) )) s ) )