#:g1: dylanでL-99 (P07 リストの平坦化)

Posted 2008-05-21 02:48:00 GMT

-(http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html)
最近の?dylanはドットで連結できるのですが、これだと見た目は、かなりLispっぽくなくなる気がします。

let seq = #(1, #(2, 3, #(4, 5, #(#(#(6, 7)),8))), 9);
format-out("%= => %=\n",seq, seq.flatten);
//=> #(1, #(2, 3, #(4, 5, #(#(#(6, 7)), 8))), 9)
//  => #(1, 2, 3, 4, 5, 6, 7, 8, 9)

// Code module: l99-07

define generic flatten (sequence :: <sequence>) => (result :: <sequence>);

define method flatten (sequence :: <list>) => (result :: <list>) case sequence.empty? => sequence; instance?(sequence.head, <list>) => concatenate(sequence.head.flatten, sequence.tail.flatten); otherwise => pair(sequence.head, sequence.tail.flatten); end end method flatten;


comments powered by Disqus