Comment detail

2進数の記述 (Nested Flatten)

This comment is reply for 4553 anekos: (2進数の記述). Go to thread root.

Template Haskell を使ってみた。 2ファイルあります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{- binmain.hs -}

import Bin

main = print $(b 01101001)



{- bin.hs -}

module Bin (b) where

import Language.Haskell.TH

bin :: Integer -> Integer
bin 0 = 0
bin x = bin' x + 2 * bin (div x 10)
    where bin' x = if odd x then 1 else 0

b x = litE $ IntegerL $ bin x

{-
>> ghc --make -fth binmain.hs 
>> ./binmain
105
-

Index

Feed

Other

Link

Pathtraq

loading...