[racket] Challenge: Game of life in 140 chars

From: Michael W (mwilber at uccs.edu)
Date: Fri Feb 17 15:40:15 EST 2012

Using vectors, this one's (life) function is about ~240 chars
after compacting whitespace.

;;;;;;;;;;;;;;;;;; 8< ;;;;;;;;;;;;;;;;;; 8< ;;;;;;;;;;;;;;;;;;
#lang racket

(define glider
  '#(#(0 0 0 0 0 0 0 0 0 0)
     #(0 0 0 0 0 0 0 0 0 0)
     #(0 0 0 0 1 0 0 0 0 0)
     #(0 0 0 0 0 1 0 0 0 0)
     #(0 0 0 1 1 1 0 0 0 0)
     #(0 0 0 0 0 0 0 0 0 0)
     #(0 0 0 0 0 0 0 0 0 0)
     #(0 0 0 0 0 0 0 0 0 0)
     #(0 0 0 0 0 0 0 0 0 0)
     #(0 0 0 0 0 0 0 0 0 0)))

(define (life board s)
  (for/vector ([r s] [R board])
     (for/vector ([c s] [C R])
               (let* ([s (λ(b x) (in-vector b (max 0 (- x 1))(min s (+ x 2))))]
                      [n (for*/sum ([R (s board r)] [C (s R c)]) C)])
                 (if (if (positive? C)
                         (<= 3 n 4)
                         (= 3 n))
                     1 0)))))

12 minutes ago, Michael W wrote:
> Hey there! Inspired by this article, I tried (quite
> unsuccessfully) to make a very short, very contrived Game of
> Life just for fun.
> http://news.ycombinator.com/item?id=357938
> 
> How can we make this shorter?
> 
> ;;;;;;;;; 8< ;;;;;;;;;;;;;; 8< ;;;;;;;;;;;;;;
> #lang racket
> 
> (define glider
>   '((0 0 0 0 0 0 0 0 0 0)
>     (0 0 0 0 0 0 0 0 0 0)
>     (0 0 0 0 1 0 0 0 0 0)
>     (0 0 0 0 0 1 0 0 0 0)
>     (0 0 0 1 1 1 0 0 0 0)
>     (0 0 0 0 0 0 0 0 0 0)
>     (0 0 0 0 0 0 0 0 0 0)
>     (0 0 0 0 0 0 0 0 0 0)
>     (0 0 0 0 0 0 0 0 0 0)
>     (0 0 0 0 0 0 0 0 0 0)))
> 
> (define (life board s)
>   (for/list ([r s])
>      (for/list ([c s])
>                (let* ([v (λ(r c) (list-ref (list-ref board (modulo r s)) (modulo c s)))]
>                       [n (count positive
>                                 (map (λ(y x)(v (- r y) (- c x)))
>                                      '(-1 -1 -1  0 0  1 1 1)
>                                      '(-1  0  1 -1 1 -1 0 1)))])
>                  (if (or (and (positive (v r c))
>                               (= 2 n))
>                          (= 3 n))
>                      1 0)))))
> 

-- 
Yell at me if you see me somewhere,
    _mike

Posted on the users mailing list.