haskell

Transform nodes with HXT using the number of <section> ancestor nodes

拟墨画扇 提交于 2020-01-04 13:28:14
问题 I'm looking to replace all title elements with h1 , h2 , ... , h6 elements depending on how many ancestors are section elements. Example input/output: Input.xml <document> <section> <title>Title A</title> <section> <title>Title B</title> </section> <section> <title>Title C</title> <section> <title>Title D</title> </section> </section> </section> </document> Output.xml <document> <section> <h1>Title A</h1> <section> <h2>Title B</h2> </section> <section> <h2>Title C</h2> <section> <h3>Title D<

Transform nodes with HXT using the number of <section> ancestor nodes

久未见 提交于 2020-01-04 13:27:58
问题 I'm looking to replace all title elements with h1 , h2 , ... , h6 elements depending on how many ancestors are section elements. Example input/output: Input.xml <document> <section> <title>Title A</title> <section> <title>Title B</title> </section> <section> <title>Title C</title> <section> <title>Title D</title> </section> </section> </section> </document> Output.xml <document> <section> <h1>Title A</h1> <section> <h2>Title B</h2> </section> <section> <h2>Title C</h2> <section> <h3>Title D<

Why doesn't Parsec backtrack when one part of the parser succeeds and the rest fails?

假如想象 提交于 2020-01-04 13:11:31
问题 I have this parsec parser : a = optionMaybe $ do {try $ spaceornull *> string "hello";string "No"} Where spaceornull is ((:[]) <$> try space) <|> string "" When I test a with input " " I get : Left (line 1, column 2): unexpected end of input expecting "hello" I don't understand this, spaceornull *> string "hello" should fail because there is no "hello", then with try parsec backtracks and now there is no consumed input but try fails anyway so the parser passed to optionMaybe (the one inside

Why doesn't Parsec backtrack when one part of the parser succeeds and the rest fails?

丶灬走出姿态 提交于 2020-01-04 13:11:07
问题 I have this parsec parser : a = optionMaybe $ do {try $ spaceornull *> string "hello";string "No"} Where spaceornull is ((:[]) <$> try space) <|> string "" When I test a with input " " I get : Left (line 1, column 2): unexpected end of input expecting "hello" I don't understand this, spaceornull *> string "hello" should fail because there is no "hello", then with try parsec backtracks and now there is no consumed input but try fails anyway so the parser passed to optionMaybe (the one inside

Could not find module 'System.Console.Readline' in Haskell

强颜欢笑 提交于 2020-01-04 12:56:10
问题 When I try to execute :load hello.hs in ghci, I get the following error: Could not find module "System.Console.Readline" Since this is a System module, I thought this would work. How do I fix this? 回答1: Depending on your system, you will want to install the readline Haskell package. To do so, make sure that you have GNU readline actually installed via apt-get (Ubuntu), macports (Mac OSX), Cygwin (Windows?), or whatever your favourite package manager is on your platform. If you have Haskell's

Automatic differentiation (AD) with respect to list of matrices in Haskell

我只是一个虾纸丫 提交于 2020-01-04 10:06:22
问题 I am trying to understand how can I use Numeric.AD (automatic differentiation) in Haskell. I defined a simple matrix type and a scalar function taking an array and two matrices as arguments. I want to use AD to get the gradient of the scoring function with respect to both matrices, but I'm running into compilation problems. Here is the code {-# LANGUAGE DeriveTraversable, DeriveFunctor, DeriveFoldable #-} import Numeric.AD.Mode.Reverse as R import Data.Traversable as T import Data.Foldable as

Sum of the first elem of a 3-tuples list

人盡茶涼 提交于 2020-01-04 09:41:11
问题 I'm new around programming stuff :/ I need to make a function that retrieve the sum of the first element from a 3-tuple list. I have something like: tuples = [(11,"11","11"),(22,"22","22"),(33,"33","33"),(44,"44","44"),(55,"55","55"),(66,"66","66")] And I need the sum of the first element of each 3-tuple from the list. = 11+22+33+44+55 Pattern matching maybe? map? 回答1: Use sum with a list comprehension: sum [x | (x, _, _) <- tuples] 回答2: If you want something pointfree, you could try: > let f

Unit-testing the undefined evaluated in lazy expression in Haskell

强颜欢笑 提交于 2020-01-04 09:03:53
问题 Writing a unit test in Haskell where an expression should fail when undefined is encountered is a bit tricky. I tried the following with HSpec: module Main where import Test.Hspec import Control.Exception (evaluate) main :: IO () main = hspec $ do describe "Test" $ do it "test case" $ do evaluate (take 1 $ map (+1) [undefined, 2, 3]) `shouldThrow` anyException to no avail. It reports me did not get expected exception: SomeException If I evaluate the same expression in REPL, I'd get: [***

Haskell tricky space overflow

流过昼夜 提交于 2020-01-04 09:03:37
问题 Running into a space overflow when trying to run this code (I've commented out the changes I've already tried): {-# LANGUAGE BangPatterns #-} import System.IO (hFlush, stdout) import System.Environment (getArgs) -- import Data.List (foldl') import qualified Data.Map as Map -- import qualified Data.Map.Strict as Map -- import qualified Data.ByteString.Char8 as B data Trie = Trie { isWord :: Bool, children :: Map.Map Char Trie } initial :: Trie initial = Trie False Map.empty insertWord ::

Template Haskell compile error when calling with different parameters

放肆的年华 提交于 2020-01-04 08:24:26
问题 Why does the following fail to compile (on GHC 7.4.2)? {-# LANGUAGE TemplateHaskell #-} f1 = $([| id |]) main = print $ (f1 (42 :: Int), f1 (42 :: Integer)) Note that the following compiles fine: {-# LANGUAGE TemplateHaskell #-} f1 = id -- Don't use template Haskell here. main = print $ (f1 (42 :: Int), f1 (42 :: Integer)) Is there a language extension I can use to make the former compile? I know the Template Haskell seems silly in this example, but it's a simplified version of a more complex