[racket] Challenge: Game of life in 140 chars

From: Jeremiah Willcock (jewillco at osl.iu.edu)
Date: Fri Feb 17 15:58:58 EST 2012

On Fri, 17 Feb 2012, Michael W wrote:

> 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)))))

You can also replace positive? by (> C 0), and change the name "board" to 
something shorter.

-- Jeremiah Willcock

>
> 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
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users
>

Posted on the users mailing list.