バイナリクロック
Posted feedbacks - Haskell
6桁固定、24時、秒まで表示ですが。。。 *Main> :main -- 15:50:14 □□■■■■ ■■□□■□ □□■■■□
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import Data.List
import Data.Time
import qualified System.IO.UTF8 as U
import System.Locale
main = getCurrentTime
>>= utcToLocalZonedTime
>>= mapM_ (U.putStrLn . repBin . toBin 6)
. read . formatTime defaultTimeLocale "[%H,%M,%S]"
repBin :: [Int] -> String
repBin = map f
where
f 1 = '■'
f 0 = '□'
toBin :: Int -> Int -> [Int]
toBin n m = snd $ mapAccumR divMod m $ replicate n 2
|
なるほど!
Haskellで。
もうちょっとスマートに書けたならなぁ。
0->'.' / 1->'o' です。
1 2 3 4 5 6 7 8 | import Data.Time
import System.Locale
digits = [a5:a4:a3:a2:a1:a0:[] | a5<-".o", a4<-".o", a3<-".o", a2<-".o", a1<-".o", a0<-".o"]
main = getCurrentTime
>>= utcToLocalZonedTime
>>= mapM_ (putStrLn.(digits!!)).read.formatTime defaultTimeLocale "[%H,%M,%S]"
|
スマートかどうかは微妙だけど...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import Data.Time
import System.Locale
import qualified System.IO.UTF8 as U
bits :: [Char]
bits = "□■"
binReps :: Int -> [[Char]]
binReps 0 = [[]]
binReps n = concatMap (flip map (binReps (n-1)) . (:)) bits
main :: IO ()
main = mapM_ (U.putStrLn . (binReps 6 !!))
. read . formatTime defaultTimeLocale "[%H,%M,%S]"
=<< utcToLocalZonedTime =<< getCurrentTime
|
binRepsの明示的再帰がちょっとスマートでない? それなら、こちらをどうぞ。
1 2 3 4 | binReps = (binReps' !!)
binReps' :: [[[Char]]]
binReps' = scanl (concatMap . (.(:)) . flip map) [[]] (repeat bits)
|
ちょ、おま (^_^;)
1 | binReps n = sequence $ replicate n bits
|
ああますますダサクしてしまった
binReps = sequence . flip replicate bits
でいいんだって。 thanks [1..100]>>=pen





lunlumo #9282() [ Ruby ] Rating6/8=0.75
20:18の場合,例えば以下の様な出力をするイメージです。
出力例:
■□■□□
□■□□■□
see: Binary Clock Widget
Rating6/8=0.75-0+
[ reply ]