Created
July 4, 2012 22:38
-
-
Save astro/3049904 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
--- twobit.hs.orig 2012-07-05 00:24:21.399187088 +0200 | |
+++ twobit.hs 2012-07-05 00:38:06.288433147 +0200 | |
@@ -1,3 +1,4 @@ | |
+{-# LANGUAGE OverloadedStrings #-} | |
import System.IO | |
import Data.Char | |
import System.Environment | |
@@ -9,7 +10,7 @@ | |
deriving (Show) | |
data V = Fun (V -> V) | |
| Val Int | |
- | Str String | |
+ | Str LBC.ByteString | |
app :: V -> V -> V | |
app (Fun f) v = f v | |
@@ -26,21 +27,27 @@ | |
church i = Fun (\f -> Fun (\x -> iterate (app f) x !! i)) | |
-- \l . l (\a b i.unchurch a : unlist b) "" | |
-unlist :: V -> String | |
+unlist :: V -> LBC.ByteString | |
unlist l = s where Str s = unlist' l | |
unlist' :: V -> V | |
unlist' l = l `app` Fun walk `app` Str "" where | |
- walk a = Fun (\b -> Fun (\i-> Str (chr (unchurch a) : unlist b))) | |
+ walk a = Fun (\b -> Fun (\i-> Str (chr (unchurch a) `LBC.cons` unlist b))) | |
cons a b = Fun (\x -> x `app` a `app` b) | |
nil = Fun (\a -> Fun (\b -> b) ) | |
-tolist "" = nil | |
-tolist (x:xs) = cons (church (ord x)) (tolist xs) | |
+tolist xs = | |
+ case LBC.uncons xs of | |
+ Just (x, xs') -> | |
+ church (ord x) `cons` tolist xs' | |
+ Nothing -> | |
+ nil | |
-type BitStream = ([Int], String) | |
+type BitStream = ([Int], LBC.ByteString) | |
stepBit :: BitStream -> (Int, BitStream) | |
stepBit (x:xs, ys) = (x, (xs, ys)) | |
-stepBit ([], y:ys) = ((ord y) `div` 128, (map (\p->(ord y) `div` 2^p `mod` 2) [6,5..0], ys)) | |
+stepBit ([], ys) = | |
+ let Just (y, ys') = LBC.uncons ys | |
+ in ((ord y) `div` 128, (map (\p->(ord y) `div` 2^p `mod` 2) [6,5..0], ys')) | |
parse :: BitStream -> (T, BitStream) | |
parse s = | |
@@ -66,8 +73,8 @@ | |
main=do | |
sources <- mapM LBC.readFile =<< getArgs | |
hSetBuffering stdout NoBuffering | |
- interact (\input-> | |
+ LBC.interact (\input-> | |
let | |
(tree, (_, rest)) = parse stream | |
- stream = ([], concat sources ++ input) | |
+ stream = ([], LBC.concat sources `LBC.append` input) | |
in unlist (eval tree [] `app` tolist rest)) | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment