haskell

Cartesian product over a list of lists in Haskell

自古美人都是妖i 提交于 2020-02-02 03:20:29
问题 Given a list of lists of length x where all the sublists have the same length y , output the y^x lists of length x that contain one item from each sublist. Example ( x = 3 , y = 2 ): [ [1, 2], [3, 4], [5, 6] ] Output ( 2^3 == 8 different outputs): [ [1, 3, 5], [1, 4, 5], [1, 3, 6], [1, 4, 6], [2, 3, 5], [2, 4, 5], [2, 3, 6], [2, 4, 6] ] My research / Work Ruby I wrote actual code to perform this task, but in Ruby, as it is the language I am most comfortable with. def all_combinations(lst) lst

Haskell: 简单解析sequenceA

痴心易碎 提交于 2020-02-02 02:31:32
Haskell: 简单解析sequenceA 文章目录 Haskell: 简单解析sequenceA sequenceA的定义 sequenceA的实用性 sequenceA的意义 参考 sequenceA的定义 -- sequenceA in Prelude sequenceA : : ( Applicative f , Traversable t ) = > t ( f a ) - > f ( t a ) -- When applied to [ ] : in which t is infered as [ ] sequenceA : : ( Applicative f ) = > [ f a ] - > f [ a ] sequenceA [ ] = pure [ ] sequenceA ( x : xs ) = ( : ) < $ > x < * > ( sequenceA xs ) -- Another definition sequenceA : : ( Applicative f ) = > [ f a ] - > f [ a ] sequenceA = foldr ( liftA2 ( : ) ) ( pure [ ] ) where liftA2 f a b = f < $ > a < * > b -- liftA2 in Control .

parsing utctime with aeson

妖精的绣舞 提交于 2020-02-01 16:06:36
问题 I can't get aeson to parse an UTCTime value. I tried to encode one and feed it back, but that didn't work: Prelude Data.Aeson Data.Time.Clock> getCurrentTime >>= (print . encode) "\"2013-10-17T09:42:49.007Z\"" Prelude Data.Aeson Data.Time.Clock> decode "2013-10-17T09:42:49.007Z" :: Maybe UTCTime Nothing Prelude Data.Aeson Data.Time.Clock> decode "\"2013-10-17T09:42:49.007Z\"" :: Maybe UTCTime Nothing The FromJSON instance of the UTCTime type is the following (ref): instance FromJSON UTCTime

parsing utctime with aeson

£可爱£侵袭症+ 提交于 2020-02-01 16:05:22
问题 I can't get aeson to parse an UTCTime value. I tried to encode one and feed it back, but that didn't work: Prelude Data.Aeson Data.Time.Clock> getCurrentTime >>= (print . encode) "\"2013-10-17T09:42:49.007Z\"" Prelude Data.Aeson Data.Time.Clock> decode "2013-10-17T09:42:49.007Z" :: Maybe UTCTime Nothing Prelude Data.Aeson Data.Time.Clock> decode "\"2013-10-17T09:42:49.007Z\"" :: Maybe UTCTime Nothing The FromJSON instance of the UTCTime type is the following (ref): instance FromJSON UTCTime

Understanding filterM

荒凉一梦 提交于 2020-02-01 11:41:56
问题 Consider filterM (\x -> [True, False]) [1, 2, 3] I just cannot understand the magic that Haskell does with this filterM use case. The source code for this function is listed below: filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys) With this use case, p should be the lambda function (\x -> [True, False]) , and the first x should be 1 . So what does flg <- p x return? What exactly

Understanding filterM

霸气de小男生 提交于 2020-02-01 11:40:15
问题 Consider filterM (\x -> [True, False]) [1, 2, 3] I just cannot understand the magic that Haskell does with this filterM use case. The source code for this function is listed below: filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys) With this use case, p should be the lambda function (\x -> [True, False]) , and the first x should be 1 . So what does flg <- p x return? What exactly

Understanding filterM

点点圈 提交于 2020-02-01 11:39:28
问题 Consider filterM (\x -> [True, False]) [1, 2, 3] I just cannot understand the magic that Haskell does with this filterM use case. The source code for this function is listed below: filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys) With this use case, p should be the lambda function (\x -> [True, False]) , and the first x should be 1 . So what does flg <- p x return? What exactly

Understanding filterM

岁酱吖の 提交于 2020-02-01 11:39:06
问题 Consider filterM (\x -> [True, False]) [1, 2, 3] I just cannot understand the magic that Haskell does with this filterM use case. The source code for this function is listed below: filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys) With this use case, p should be the lambda function (\x -> [True, False]) , and the first x should be 1 . So what does flg <- p x return? What exactly

Using optparse-applicative with multiple subcommands and global options

雨燕双飞 提交于 2020-02-01 06:55:30
问题 I am writing a commandline program that takes multiple subcommands, which take flags/arguments. The program should also take some 'global-flags' that are applicable to all subcommands. For examples: myProgram --configfile=~/.customrc UPLOADFILE --binary myfile.x myProgram --configfile=~/.customrc SEARCH --regex "[a-z]+" in this example, the subcommands are UPLOADFILE and SEARCH , and configfile is relevant to both subcommands, and binary and regex applicable to the specific subcommands. I

Generating HTML output from criterion

坚强是说给别人听的谎言 提交于 2020-02-01 01:39:22
问题 There is a nice example of HTML output from criterion at http://bos.github.com/criterion/. Which command line option is used to generate this output? An answer to a related question asserts that this output exits, but it does not seem to show up in the command line options when using --help . 回答1: Well if you just want html output, then yourBench -o yourReport.html will generate some perfectly reasonable output. If you want to use your own template, look at the templates/report.tpl example in