25 Mar 2013


posted by Asumu Takikawa

About a month ago, inspired by a mailing list post by Tim Brown, Racketeers started to write more solutions to Rosetta Code tasks for Racket. Just today, we’ve reached 200 entries in the Racket category!

This is a nice milestone, but we still have a ways to go. At 200 entries, Racket comes in at around 54th in the popularity rankings. So if you’re looking to practice your Racketeering skills, don’t hesitate to work on some of the remaining tasks.

To give you a taste of the kinds of solutions we have so far, here are some examples.


(define (iterations a z i)
  (define z′ (+ (* z z) a))
  (if (or (= i 255) (> (magnitude z′) 2))
      (iterations a z′ (add1 i))))

(define (iter->color i)
  (if (= i 255)
      (make-object color% "black")
      (make-object color% 
        (* 5 (modulo i 15)) (* 32 (modulo i 7)) 
          (* 8 (modulo i 31)))))

(define (mandelbrot width height)
  (define target (make-screen-bitmap width height))
  (define dc (new bitmap-dc% [bitmap target]))
  (for* ([x width] [y height])
    (define real-x (- (* 3.0 (/ x width)) 2.25))
    (define real-y (- (* 2.5 (/ y height)) 1.25))
    (send dc set-pen 
          (make-rectangular real-x real-y) 0 0)) 1 'solid)
    (send dc draw-point x y))

> (mandelbrot300200)

Yin and Yang:

(define (yin-yang d)
  (define base
    (hc-append (inset/clip (circle d) 0 0 (- (/ d 2)) 0)
               (inset/clip (disk d) (- (/ d 2)) 0 0 0)))
  (define with-top
     (cc-superimpose (colorize (disk (/ d 2)) "white")
                     (disk (/ d 8)))))
  (define with-bottom
     (cc-superimpose (disk (/ d 2))
                     (colorize (disk (/ d 8)) "white"))))
  (cc-superimpose with-bottom (circle d)))

> (yin-yang 200)

Animate a pendulum:

#lang racket

(require 2htdp/image

(define (pendulum)
  (define (accel θ) (- (sin θ)))
  (define θ (/ pi 2.5))
  (define θ′ 0)
  (define θ′′ (accel (/ pi 2.5)))
  (define (x θ) (+ 200 (* 150 (sin θ))))
  (define (y θ) (* 150 (cos θ)))
  (λ (n)
    (define p-image 
          (empty-scene 400 200) 200 0 (x θ) (y θ) "black")
            (- (x θ) 5) (- (y θ) 5) 
              (circle 5 "solid" "blue")))
    (set! θ (+ θ (* θ′ 0.04)))
    (set! θ′ (+ θ′ (* (accel θ) 0.04)))

(animate (pendulum))

Jensen's Device:

#lang algol60
   integer i;
   real procedure sum (i, lo, hi, term);
      value lo, hi;
      integer i, lo, hi;
      real term;
      comment term is passed by-name, and so is i;
      real temp;
      temp := 0;
      for i := lo step 1 until hi do
         temp := temp + term;
      sum := temp
   comment note the correspondence between 
   the mathematical notation and the call to sum;
   printnln (sum (i, 1, 100, 1/i))

Thanks to all of the people who have contributed solutions so far!

Racket is climbing the rankings …


Daniel Prager, 7 June 2013

Made with Frog, a static-blog generator written in Racket.
Source code for this blog.