lambdaway
::
sierpinsky4
4
|
list
|
login
|
load
|
|
_h1 sierpinsky {sup ([[triangle|?view=sierpinsky]] | carpet {sup ([[2|?view=sierpinsky2]]|[[3|?view=sierpinsky3]]|4|[[5|?view=sierpinsky5]]|[[6|?view=sierpinsky6]]|[[7|?view=sierpinsky7]])}} _p A version built essentially on text rewriting, using Scheme and Lambdatalk. _h2 1) scheme/racket version _p A code written by [[Jean-Paul Roy|http://jean.paul.roy.free.fr/]]. ;; « {i La fractale sierpinsky(n) contient 8 fois la fractale sierpinsky(n-1), le code Python s'en déduit, ainsi que le code Scheme...} » {pre (define (repeat n f init) ; appliquer la transformation f n fois sur init (if (= n 0) init (repeat (- n 1) f (f init)))) ;; (repeat 30 cos 1) ; une solution approchée de cos(x) = x (define (replace old new L) ; sans doute une primitive mais... (if (empty? L) L (cons (if (equal? (first L) old) new (first L)) (replace old new (rest L))))) (define str3 string-append) (define (transf L) (append (map (lambda (x) (str3 x x x)) L) (map (lambda (x) (str3 x (string-replace x "#" " ") x)) L) (map (lambda (x) (str3 x x x)) L))) ; recalculé, bof... (define (sierpinsky n) (repeat n transf (list "#"))) (sierpinsky 3) ; ou bien : (for [(x (sierpinsky 3))] (println x)) } _p displays {pre {@ style="line-height:0.8em;"} '("###########################" "# ## ## ## ## ## ## ## ## #" "###########################" "### ###### ###### ###" "# # # ## # # ## # # #" "### ###### ###### ###" "###########################" "# ## ## ## ## ## ## ## ## #" "###########################" "######### #########" "# ## ## # # ## ## #" "######### #########" "### ### ### ###" "# # # # # # # #" "### ### ### ###" "######### #########" "# ## ## # # ## ## #" "######### #########" "###########################" "# ## ## ## ## ## ## ## ## #" "###########################" "### ###### ###### ###" "# # # ## # # ## # # #" "### ###### ###### ###" "###########################" "# ## ## ## ## ## ## ## ## #" "###########################") } _h2 2) lambdatalk version _p See the version using lists in [[sierpinsky5]]. _p Here we forget lists and use two primitives {code [S.replace & S.map]} working on (sequences of) words and two HTML primitives, {code [h2 & div]}, for formatting. {pre '{S.replace x by _ in axbxcxdx exfxgx} -> {S.replace x by _ in axbxcxdx exfxgx} '{S.map {lambda {:x} :x:x:x} abcd efg} -> {S.map {lambda {:x} :x:x:x} abcd efg} } _p Nothing more! _h3 2.1) code {pre '{def sierpinsky {def sierpinsky.r {lambda {:n :w} {if {= :n 0} then :w else {sierpinsky.r {- :n 1} {S.map {lambda {:x} :x:x:x} :w} {S.map {lambda {:x} :x{S.replace ■ by o in :x}:x} :w} {S.map {lambda {:x} :x:x:x} :w} }}}} {lambda {:n} {h2 n=:n}{S.replace o by space in {S.replace \s by {div} in {sierpinsky.r :n ■}}}}} -> {def sierpinsky {def sierpinsky.r {lambda {:n :w} {if {= :n 0} then :w else {sierpinsky.r {- :n 1} {S.map {lambda {:x} :x:x:x} :w} ;; {S.map {lambda {:x} :x{{lambda {:x ■} :x} :x o}:x} :w} {S.map {lambda {:x} :x{S.replace ■ by o in :x}:x} :w} {S.map {lambda {:x} :x:x:x} :w} }}}} {lambda {:n} {h2 S:n}{S.replace o by space in {S.replace \s by {div} in {sierpinsky.r :n ■}}}}} } _p {b Notes} _ul 1) the last argument of a lambda, here {code :w}, gets all the following values. _ul 2) {b '{S.replace ■ by o in :x}} could be written {b '{{lambda {:x ■} :x} :x o}} _h3 2.2) tests {pre '{pre {@ style="line-height:0.6em; // to glue the lines color:#f80;"} // and color the characters {S.map sierpinsky 0 1 2 3 4} } } _p displays {pre {@ style="line-height:0.6em; color:#a00;"} {S.map sierpinsky 0 1 2 3 4} } _p These are not images but pure text blocks. _p Computation time on a 2011 MacBook Air: ≈10ms for n=3, ≈20ms for n=4. {center S4 is to be compared with the S3 [[SVG version|?view=sierpinsky3]], which is ≈10 times slower.} _img data/sierpinsky_screen.jpg {style pre { box-shadow:0 0 8px #000; padding: 10px; } }
lambdaway v.20211111