#:g1: dylanでL-99 (P10 ランレングス圧縮)

Posted 2008-06-12 08:07:00 GMT

-(http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html)
前回定義した、packの補助関数のpack1とjoinを使っています。
dylanのライブラリの取り込み方と定義方法が良く分からない…。

#(a:, a:, a:, a:, b:, c:, c:, a:, a:, d:, e:, e:, e:, e:).encode
//=> #(#(4, a:), #(1, b:), #(2, c:), #(2, a:), #(1, d:), #(4, e:))

#[a:, a:, a:, a:, b:, c:, c:, a:, a:, d:, e:, e:, e:, e:].encode //=> #[#(4, a:), #(1, b:), #(2, c:), #(2, a:), #(1, d:), #(4, e:)]

"aaaabccaadeeee".encode //=> "4a;1b;2c;2a;1d;4e"

// Code: define generic encode (sequence :: <sequence>) => (result :: <sequence>);

define method encode (sequence :: <list>) => (result :: <list>) as(<list>,next-method()) end method encode;

define method encode (sequence :: <vector>) => (result :: <vector>) as(<vector>,next-method()) end method encode;

define method encode (sequence :: <string>) => (result :: <string>) join(map(method(x) format-to-string("%d%s", x.size, x.first) end, sequence.pack1), ";") end method encode;

define method encode (sequence :: <sequence>) => (result :: <sequence>) map(method(x) list(x.size, x.first) end, sequence.pack1) end method encode;


comments powered by Disqus