Last active
December 17, 2023 22:42
-
-
Save yamasushi/472c444409e8bd519e76bf0325398715 to your computer and use it in GitHub Desktop.
positive binary number
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
import Text.Printf | |
import Numeric | |
import Data.Char | |
-- positive binary number | |
-- https://gist.github.com/yamasushi/472c444409e8bd519e76bf0325398715 | |
-- B -> B1 0 { B.syn = 2 * B1.syn } | |
-- | B1 1 { B.syn = 2 * B1.syn + 1} | |
-- | 1 {B.syn = 1} | |
-- | |
-- B -> 1 {R.inh = 1} R {B.syn = R.syn} | |
-- R -> 0 {R1.inh = R.inh * 2} R1 {R.syn = R1.syn} | |
-- R -> 1 {R1.inh = R.inh * 2 + 1} R1 {R.syn = R1.syn} | |
-- R -> ε {R.syn = R.inh} | |
test :: Int -> IO() | |
test num = | |
do | |
let str = showIntAtBase 2 intToDigit num "" | |
printf "%b(%d) --- %b\n" (num :: Int) (num::Int) (fst. b $ str) | |
b :: String -> (Int,String) | |
b ('1':rest) = r 1 rest | |
r :: Int->String->(Int,String) | |
r inh [] = (inh,[]) -- ε | |
r inh ('0':rest) = r (inh*2) rest | |
r inh ('1':rest) = r (inh*2+1) rest | |
r inh xs = (inh,xs) -- ε |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment