Created
September 8, 2013 08:12
-
-
Save ktakashi/6482844 to your computer and use it in GitHub Desktop.
Code IQ hello world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!r6rs | |
(import (only (rename (rnrs) (cons %cons) (+ %+)) | |
define lambda let display newline integer->char length | |
quote %cons)) | |
(define cons (lambda (x y) (lambda (z) (z x y)))) | |
(define car (lambda (z) (z (lambda (x y) x)))) | |
(define cdr (lambda (z) (z (lambda (x y) y)))) | |
(define true (lambda (x y) (x))) | |
(define false (lambda (x y) (y))) | |
(define if (lambda (x y z) (x y z))) | |
;; 0 = (#t . _) | |
;; 1 = (#f #t . _) | |
(define zero (cons true false)) | |
(define zero? (lambda (x) (car x))) | |
(define inc (lambda (x) (cons false x))) | |
(define dec (lambda (x) (cdr x))) | |
(define + (lambda (x y) | |
(if (zero? x) | |
(lambda () y) | |
(lambda () (+ (dec x) (inc y)))))) | |
(define * (lambda (x y) | |
(if (zero? x) | |
(lambda () zero) | |
(lambda () (+ y (* (dec x) y)))))) | |
;; hello world | |
;; -> (104 101 108 108 111 32 119 111 114 108 100) | |
;; -> ((100 + 4) ... 32 (100 + 19) ...) | |
(define %1 (inc zero)) | |
(define %2 (inc %1)) | |
(define %3 (inc %2)) | |
(define %4 (inc %3)) | |
(define %5 (inc %4)) | |
(define %100 (* (* (* %5 %5) %2) %2)) | |
;; helper | |
(define ->list (lambda (n) | |
(let loop ((n n) (r '())) | |
(if (zero? n) | |
(lambda () r) | |
(lambda () (loop (dec n) (%cons #t r))))))) | |
(define (put-char n) | |
(display (integer->char (length (->list n))))) | |
(put-char (+ %100 %4)) | |
(put-char (+ %100 %1)) | |
(put-char (+ %100 (* %4 %2))) | |
(put-char (+ %100 (* %4 %2))) | |
(put-char (+ %100 (+ (* %5 %2) %1))) | |
(put-char (+ (* %5 %5) (+ %3 %4))) | |
(put-char (+ %100 (dec (* (* %5 %2) %2)))) | |
(put-char (+ %100 (inc (* %5 %2)))) | |
(put-char (+ %100 (dec (* %5 %3)))) | |
(put-char (+ %100 (* %4 %2))) | |
(put-char %100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment