Created
September 21, 2013 08:07
-
-
Save h-hirai/6648418 to your computer and use it in GitHub Desktop.
Hello World program without Chars, Strings, Numbers.
https://codeiq.jp/ace/cielavenir/q431
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
module Main where | |
import Data.Char (chr) | |
import System.Exit | |
import Data.Word | |
-- numbers | |
num0 :: Int | |
num0 = fromIntegral (minBound::Word8) | |
num256 :: Int | |
num256 = realize $ eval church256 | |
-- Lazy K interpreter, written by @fumieval | |
-- http://ideone.com/ST3kF | |
infixl :$ | |
data Expr = Expr :$ Expr|I|K|S|Inc|Export {-# UNPACK #-} !Int deriving Show | |
apply :: Expr -> Expr -> Expr | |
apply (S :$ x :$ y) z = apply x z `apply` apply y z | |
apply (K :$ x) _ = x | |
apply I x = x | |
apply Inc (Export x) = Export $! succ x | |
apply f x = f :$ x | |
eval :: Expr -> Expr | |
eval (x :$ y) = eval x `apply` eval y | |
eval x = x | |
cons :: Expr -> Expr -> Expr | |
cons a b = S :$ (S :$ I :$ (K :$ a)) :$ (K :$ b) | |
export :: Expr -> Int | |
export (Export x) = x | |
realize :: Expr -> Int | |
realize expr = export $ expr `apply` Inc `apply` Export num0 | |
output :: Expr -> IO () | |
output expr | |
| x < num256 = putChar (chr x) >> (output $ apply expr $ K :$ I) | |
| x == num256 = exitWith ExitSuccess | |
| otherwise = exitWith $ ExitFailure $ x - num256 | |
where | |
x = realize $ apply expr K | |
-- encoding to `51b number' | |
-- http://d.hatena.ne.jp/rst76/20121204/1354629448 | |
nbHelloWorld :: [Expr] | |
nbHelloWorld = [ | |
(S :$ ((S :$ S) :$ S)) :$ (((S :$ ((S :$ (K :$ S)) :$ (S :$ S))) :$ I) :$ ((S :$ S) :$ (S :$ K))), | |
(S :$ S) :$ ((S :$ ((S :$ S) :$ (S :$ K))) :$ ((S :$ S) :$ ((((S :$ S) :$ S) :$ S) :$ ((S :$ S) :$ (S :$ K))))), | |
(S :$ ((S :$ S) :$ ((S :$ S) :$ ((S :$ S) :$ S)))) :$ ((S :$ S) :$ ((S :$ S) :$ (S :$ K))), | |
(S :$ ((S :$ S) :$ ((S :$ S) :$ ((S :$ S) :$ S)))) :$ ((S :$ S) :$ ((S :$ S) :$ (S :$ K))), | |
(S :$ S) :$ ((S :$ ((S :$ S) :$ S)) :$ ((S :$ S) :$ ((((S :$ S) :$ S) :$ S) :$ ((S :$ S) :$ (S :$ K))))), | |
(S :$ ((S :$ S) :$ (((S :$ S) :$ I) :$ ((S :$ S) :$ (S :$ K))))) :$ ((S :$ S) :$ (S :$ K)), | |
(S :$ ((S :$ ((S :$ ((S :$ S) :$ S)) :$ ((S :$ S) :$ (S :$ K)))) :$ S)) :$ ((((((S :$ S) :$ S) :$ S) :$ S) :$ S) :$ ((S :$ S) :$ (S :$ K))), | |
(S :$ S) :$ ((S :$ ((S :$ S) :$ S)) :$ ((S :$ S) :$ ((((S :$ S) :$ S) :$ S) :$ ((S :$ S) :$ (S :$ K))))), | |
(S :$ S) :$ ((S :$ S) :$ ((S :$ S) :$ ((S :$ S) :$ ((S :$ ((S :$ S) :$ S)) :$ ((S :$ S) :$ ((((S :$ S) :$ S) :$ S) :$ ((S :$ S) :$ (S :$ K)))))))), | |
(S :$ ((S :$ S) :$ ((S :$ S) :$ ((S :$ S) :$ S)))) :$ ((S :$ S) :$ ((S :$ S) :$ (S :$ K))), | |
(S :$ ((S :$ S) :$ (S :$ K))) :$ ((S :$ S) :$ ((((S :$ S) :$ S) :$ S) :$ ((S :$ S) :$ (S :$ K))))] | |
church256 :: Expr | |
church256 = S :$ I :$ I :$ (S :$ I :$ I :$ (S :$ (S :$ (K :$ S) :$ K) :$ I)) | |
helloWorld :: Expr | |
helloWorld = foldr cons (cons church256 church256) $ | |
map (:$ (S :$ (K :$ S) :$ K)) nbHelloWorld | |
main :: IO () | |
main = output $ eval helloWorld |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment