Skip to content

Instantly share code, notes, and snippets.

@gregorycollins
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregorycollins/9103248 to your computer and use it in GitHub Desktop.
Save gregorycollins/9103248 to your computer and use it in GitHub Desktop.
Haskell-cafe thread on Feb 19 2014
-- Initial h.cabal generated by cabal init. For further documentation, see
-- http://haskell.org/cabal/users-guide/
name: h
version: 0.1.0.0
-- synopsis:
-- description:
-- license:
author: Gregory Collins
maintainer: greg@gregorycollins.net
-- copyright:
-- category:
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable h1
main-is: h1.hs
ghc-options: -Wall -fwarn-tabs -funbox-strict-fields -O2 -fno-warn-unused-do-bind -rtsopts
build-depends: base >=4.5 && <4.6,
bytestring
default-language: Haskell2010
executable h2
main-is: h2.hs
ghc-options: -Wall -fwarn-tabs -funbox-strict-fields -O2 -fno-warn-unused-do-bind -rtsopts
build-depends: base >=4.5 && <4.6,
bytestring,
blaze-builder,
io-streams
default-language: Haskell2010
executable h3
main-is: h3.hs
ghc-options: -Wall -fwarn-tabs -funbox-strict-fields -O2 -fno-warn-unused-do-bind -rtsopts
build-depends: base >=4.5 && <4.6,
bytestring,
blaze-builder,
io-streams
default-language: Haskell2010
module Main where
import qualified Data.ByteString as BS
import System.Environment
import Data.Word
only012 :: Word64 -> Bool
only012 0 = True
only012 n = (q < 3) && only012 p
where (p,q) = n `divMod` 10
go :: Word64 -> IO ()
go 0 = return ()
go n = if only012 n
then (BS.putStr $ BS.pack ([1] :: [Word8])) >> go (n-1)
else (BS.putStr $ BS.pack ([0] :: [Word8])) >> go (n-1)
main = do
[d] <- map read `fmap` getArgs
go d
{-# LANGUAGE BangPatterns #-}
module Main where
import Blaze.ByteString.Builder as Blaze
import Blaze.ByteString.Builder.Internal.Buffer as Blaze
import qualified Data.ByteString as BS
import System.Environment
import qualified System.IO.Streams as Streams
import Data.Word
only012 :: Word64 -> Bool
only012 0 = True
only012 n = (q < 3) && only012 p
where (!p,!q) = n `divMod` 10
go :: Word64 -> IO ()
go i = do
buf <- Blaze.allocBuffer 4096
os <- Streams.unsafeBuilderStream (return buf) Streams.stdout
f os i
where
f os i = g i
where
g 0 = return ()
g n = do
let !c = if only012 n then 1 else (0::Word8)
Streams.write (Just $! Blaze.fromWord8 c) os
g (n-1)
main = do
[d] <- map read `fmap` getArgs
go d
{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.ByteString as BS
import System.Environment
import Data.Word
only012 :: Word64 -> Bool
only012 0 = True
only012 n = (q < 3) && only012 p
where (!p,!q) = n `divMod` 10
go :: Word64 -> IO ()
go 0 = return ()
go n = if only012 n
then (BS.putStr $ BS.pack ([1] :: [Word8])) >> go (n-1)
else (BS.putStr $ BS.pack ([0] :: [Word8])) >> go (n-1)
main = do
[d] <- map read `fmap` getArgs
go d
import Distribution.Simple
main = defaultMain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment