[2015-09-07] Challenge #213 [Easy] Cellular Automata: Rule 90

Wrote the program using common lisp. Converts the string into a linked list and uses simple recursion to step through the elements. #lang racket ;; String -> String ;; Applies rule 90 to every number within the string, and returns the new string (define (xor-line line) (letrec [ ;; char -> char : a simple char XOR operation (xor (λ (num1 num3) (if (not (char=? num1 num3)) #\1 #\0))) ;; String -> [List-of char] : Converts string to an easier list to iterate through (line->list (string->list line)) ;; [List-of char] -> [List-of char] : Applies xor to every char in the list, while ;; updating next and prev (helper (λ (list prev next) (if (= (length list) 2) (cons (xor prev next) (cons (xor (first list) #\0) empty)) (cons (xor prev next) (helper (rest list) (first list) (third list)))))) ;; [List-of char] -> [List-of char] : Makes sure no bad input gets tossed into helper (help (λ (list) (if (<(length list) 3) list (helper list #\0 (second list)))))] (list->string (help line->list))))

;; String -> Void
;; Modifies the display function to simply replaces 1 with X and 0 with " ", prints to console
(define (display-x arg)
  (displayln (foldr string-append "" (map (λ (c) (if (char=? c #\1) "X" " ")) (string->list arg)))))

;; (A -> A) A Integer -> A
;; Applies a function to its own result the desired amount of times. 
(define (repeat1 func arg counter)
  (if (<= counter 0) (void)
      (begin (display-x arg) (repeat1 func (func arg) (sub1 counter)))))


(repeat1 xor-line "00000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000" 25)
(repeat1 xor-line "1101010" 6)

Leads to X
X X
X X
X X X X
X X
X X X X
X X X X
X X X X X X X X
X X
X X X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X X X X X X X X X
X X
X X X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X
XX X X XX X XXX X X XXX X X X
X X

/r/dailyprogrammer Thread