aDestでMDをMの前に持ってきたらどうでしょうか
asum
試してみます<|>
でつなげる順序に依存しない方法でいきたいのです=
まで見るようにしないといけないaDest = M <$ "M" <* char '=' <|> D <$ "D" <* char '=' <|> MD <$ "MD" <* char '='
aDest = asum . map (<* char '=') $ [ M <$ "M" , D <$ "D" , MD <$ "MD" ]
-- AtCoderPrelude.hs module AtCoderPrelude (readUVecLn, parseInt, otherfct) where import Control.Monad.State.Strict (StateT, runStateT) import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as BSC import qualified Data.Vector.Unboxed as VU import qualified Some.Random.Module as M -- | Given a parser, parse a whole input line and give a vector. readUVecLn :: (VU.Unbox a) => Int -- ^ Expected maximum length -> StateT BS.ByteString Maybe a -- ^ Parser -> IO (VU.Vector a) readUVecLn len parser = VU.unfoldrN len (runStateT parser) <$> BS.getLine -- | Skip spaces and control chars, and parse an Int. parseInt :: StateT BS.ByteString Maybe Int parseInt = StateT $ BSC.readInt . BS.dropWhile (< fromIntegral (fromEnum '!')) otherfct :: A -> Very -> Useful -> Function otherfct = M.usefulFunction "SomeGoodString"
-- Main.hs import AtCoderPrelude main :: IO () main = do n <- readLn vec <- readUVecLn n parseInt print vec
-- Merged.hs import qualified Control.Monad.State.Strict (StateT, runStateT) as M3a6f5d import qualified Data.ByteString as M65a7e5 import qualified Data.ByteString.Char8 as M47ef44 import qualified Data.Vector.Unboxed as M3266e4 readUVecLn_M2346ed :: (M3266e4.Unbox a) => Int -> M3a6f5d.StateT M65a7e5.ByteString Maybe a -> IO (M3266e4.Vector a) readUVecLn_M2346ed len parser = M3266e4.unfoldrN len (M3a6f5d.runStateT parser) <$> M65a7e5.getLine parseInt_M2346ed :: M3a6f5d.StateT M65a7e5.ByteString Maybe Int parseInt_M2346ed = M3a6f5d.StateT $ M47ef44.readInt . M65a7e5.dropWhile (< fromIntegral (fromEnum '!') main :: IO () main = do n <- readLn vec <- readUVecLn_M2346ed n parseInt_M2346ed print vec
{-# LANGUAGE CPP #-} #include "AtCoderPrelude.hs"
cpphs --noline Main.hs
stack.yaml
とかにextra-depsとしてリポジトリーやtarballのURLを書いておき)、Numeric.LinearPrimeSieve
みたいなモジュールもあるので、単純に sed
だとうまくいかないのが難点ですけどね・・・。import ... as
に対応するのが面倒、とか、色々面倒だなあ、と思ったんですよね・・・。millisSinceEpoch :: UTCTime -> Int millisSinceEpoch = floor . (1e3 *) . nominalDiffTimeToSeconds . utcTimeToPOSIXSeconds main :: IO () main = do start <- millisSinceEpoch <$> getCurrentTime threadDelay $ 2 * 10 ^ 6 end <- millisSinceEpoch <$> getCurrentTime print . show $ (end - start)
rnf
と evaluate
を組み合わせて使いましょう。newtype Difference = Difference { unDifference :: Int } deriving stock (Eq, Ord, Bounded) deriving newtype (Enum, Show, Read, Num, Real, Integral) deriving (Semigroup, Monoid) via Sum Int
src\Numeric\YHSeq\V0201.hs:32:15: error: • Couldn't match representation of type 'Int' with that of 'Sum Int' arising from the coercion of the method '<>' from type 'Sum Int -> Sum Int -> Sum Int' to type 'Difference -> Difference -> Difference' The data constructor 'base-4.12.0.0:Data.Semigroup.Internal.Sum' of newtype 'Sum' is not in scope • When deriving the instance for (Semigroup Difference) | 32 | deriving (Semigroup, Monoid) via Sum Int |
{-# LANGUAGE DerivingVia #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module A where -- import Data.Monoid (Sum) -- 同様のコンパイルエラー import Data.Monoid (Sum (Sum)) -- コンパイルできる newtype Difference = Difference { unDifference :: Int } deriving stock (Eq, Ord, Bounded) deriving newtype (Enum, Show, Read, Num, Real, Integral) deriving (Semigroup, Monoid) via Sum Int
The data constructor ‘base-4.12.0.0:Data.Semigroup.Internal.Sum’って書いてある通りですね。
of newtype ‘Sum’ is not in scope
Sum Int
の中身が Int
だと知ることができない、って感じだと思われます。head
についてお聞きしたいです。head
は最初の要素だけなんでしょうか? 関数型言語には利用必須ですが、`head` を`head :: [a] -> Int -> [a]` のような形にして、`last` があるので head
の内部実装をそのまま で first
を作ってもよかったんじゃないかと思いました。