[racket] 2htdp/image Sierpinski
I adapted the Sierpinski example from the racket-lang.org home page for
Intermediate Student Language. The parameter represents recursive depth,
which requires no division and a single local definition. If it weren't for
the local, this would be a BSL program:
(require 2htdp/image)
;; Examples
(define st0 (triangle 16 'solid 'red))
(define st1 (above st0 (beside st0 st0)))
;; Tests
(check-expect (ski 0) st0)
(check-expect (ski 1) st1)
;; number -> image
;; Draws Sierpinski Triangle to given recursive depth
(define (ski n)
(cond
[(zero? n) (triangle 16 'solid 'red)]
[else (local ((define next (ski (- n 1))))
(above next (beside next next)))]))
;; Draw Sierpinski Triangles to different recursive depths
(ski 0)
(ski 1)
(ski 2)
(ski 3)
Hope this helps,
Marc
Gregor Kiczales wrote:
> Date: Sat, 30 Oct 2010 21:18:43 -0700
From: Gregor Kiczales <gregor at cs.ubc.ca>
To: users at racket-lang.org
Subject: [racket] 2htdp/image Sierpinski
Message-ID: <EFC7275A-B06E-463F-AE96-EA9E0C36CCC7 at cs.ubc.ca>
Content-Type: text/plain; charset=us-ascii
> In 2htdp/image the implementations of Sierpinski triangle and carpet are
> nice and simple, which is great for using them in class.
>
> (define (carpet size c)
(if (<= size 2)
(square size "outline" c)
(local [(define sub (carpet (/ size 3) c))
(define ctr (square (/ size 3) "solid" "white"))]
(overlay (square size "outline" c)
(above (beside sub sub sub)
(beside sub ctr sub)
(beside sub sub sub))))))
> But... (carpet 200 "red") produces some strange behavior on my Mac.
> (carpet 300 "red") is even worse. The image draws very slowly, which I
> assume comes from the way the image is actually composed of all the composed
> images rather than a single bitmap. But, stranger than that, it seems to
> need to draw itself several times.
> Also I would have thought that 'forcing' the image by actually drawing it
> would save the bitmap so that future drawing was faster. But I may not
> understand how this is actually working.
> Is this style of using above, beside and friends to make something this big
> intended to work? Or is there a different style I should be using?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20101031/66ee0302/attachment.html>