haskell

Parsing single qoute char in a single-quoted string in parsec

ε祈祈猫儿з 提交于 2019-12-25 03:27:18
问题 I've got a silly situation in my parsec parsers that I would like your help on. I need to parse a sequence of strongs / chars that are separated by | characters. So, we could have a|b|'c'|'abcd' which should be turned into [a,b,c,abcd] Space is not allowed, unless inside of a ' ' string. Now, in my naïve attempt, I got the situation now where I can parse strings like a'a|'bb' to [a'a,bb] but not aa|'b'b' to [aa,b'b]. singleQuotedChar :: Parser Char singleQuotedChar = noneOf "'" <|> try

Parsing single qoute char in a single-quoted string in parsec

非 Y 不嫁゛ 提交于 2019-12-25 03:27:09
问题 I've got a silly situation in my parsec parsers that I would like your help on. I need to parse a sequence of strongs / chars that are separated by | characters. So, we could have a|b|'c'|'abcd' which should be turned into [a,b,c,abcd] Space is not allowed, unless inside of a ' ' string. Now, in my naïve attempt, I got the situation now where I can parse strings like a'a|'bb' to [a'a,bb] but not aa|'b'b' to [aa,b'b]. singleQuotedChar :: Parser Char singleQuotedChar = noneOf "'" <|> try

Functional Banana Traveller - Input Handling

こ雲淡風輕ζ 提交于 2019-12-25 03:26:33
问题 This is a sub-problem from my Traveller project. I've put together the rudementary code that will handle input. It works, until I introduce a TChan to the mix. Below is the working code, with an example of how to use it. Then, I will change it and explain why I am doing so. Then, I'll talk about the problem. {-# LANGUAGE ScopedTypeVariables #-} import Control.Monad (forever) import Control.Concurrent (forkIO) import Control.Monad.STM (STM,atomically) import Control.Concurrent.STM.TChan import

GLR_Lib.hs: Could not find module 'System'

烂漫一生 提交于 2019-12-25 02:53:40
问题 I am trying to generate a GLR parser from happy, but I am getting errors once the files are generated. Here is an example, ABC.y , so it's clear what I am trying: { module Main where } %name ps1 s1 %tokentype { ABC } %error { parseError } %token a { A } b { B } c { C } %% s1: a a a b {} | b s2 a {} s2: b a b s2 {} | c {} { data ABC = A | B | C parseError _ = error "bad" main = getContents >>= print . ps1 . lexer lexer ('a':xs) = A : lexer xs ETC } This example works just fine with happy ABC.y

How to nicely denote the alternative of three conditions?

假装没事ソ 提交于 2019-12-25 02:33:22
问题 Assume I have a character c :: Char . Now I want to see if it's equal to a or isDigit : isAOrDigit = (||) <$> (=='a') <*> (isDigit) So far so good. But now I want to see if it's equal to a , isDigit or is between d and g . Unfortunately, since || only accepts 2 arguments, I can't say (||) <$> (=='a') <*> (isDigit) <*> (`elem`['d'..'g']) . Is there any nice way to write this or do i have to fall back to this: isACOrDigitOrDG c = c == 'a' || isDigit c || c `elem` ['d'..'g'] ? 回答1: You can use

Manual derivation of the type for `f1 x xs = (filter . (<)) x xs`

旧巷老猫 提交于 2019-12-25 02:19:32
问题 I want to manually derive the type of: f1 x xs = (filter . (<)) x xs First time we see x , so: x :: t1 Then (<) has this type: (<) :: Ord a1 => a1 -> a1 -> Bool We can only say (< x) if the following types can be unified: t1 ~ a1 Then x :: a1 So (<x) :: Ord a1 => a1 -> Bool Filter has this type filter :: (a2 -> Bool) -> [a2] -> [a2] First time to see xs, so: xs :: t2 We can only say (filter . (<)) x xs if the following types can be unified: a1 -> Bool ~ a2 -> Bool t2 ~ [a2] So I get that f1 :

Haskell - Non-exhaustive patterns in case

做~自己de王妃 提交于 2019-12-25 02:17:03
问题 I have got the following code: F (S core ps) = FAll core [] ps where FAll core acc ((name, (pc : pcs)) : ps) = case F' (pc : pcs) (readC pc core) core of Nothing -> if (length pcs) /= 0 then FAll core ((name, pcs) : acc) ps else FAll core acc ps Just (core', [pc']) -> let pc'' = pc' `mod` coresize pcs' = pcs ++ [pc''] in FAll core' ((name, pcs') : acc) ps stepAll core acc [] = S core (reverse acc) It compiles fine but when I run the program it gives the following error: Melon.hs:(172,10)-(182

(typed) list of argument from a program using optparse-appplicative

旧街凉风 提交于 2019-12-25 02:16:27
问题 Is there a way to extract a list of names and types from a command line program, made using optparse-applicative ? I am +/- looking for some function of type ParserInfo a -> [(String,TypeRep)] 回答1: No, there is no way. The relevant bits are: data ParserInfo a = ParserInfo { infoParser :: Parser a , -- ... } data Parser a = forall x . MultP (Parser (x -> a)) (Parser x) | forall x . BindP (Parser x) (x -> Parser a) | -- ... Since the x s of MultP and BindP are existentially quantified and do

Haskell Parsec accounting for multiple expression occrrences in grammar

笑着哭i 提交于 2019-12-25 01:54:10
问题 I have been trying to create a parser using details from the following tutorial much of the code is copied directly from the tutorial with only a few names changed. import qualified Text.ParserCombinators.Parsec.Token as P reserved = P.reserved lexer integer = P.integer lexer whiteSpace = P.whiteSpace lexer identifier = P.identifier lexer data Express = Seq [Express] | ID String | Num Integer | BoolConst Bool deriving (Show) whileParser :: Parser Express whileParser = whiteSpace >> expr7

Applicative Distributor for List `dist` Function

泪湿孤枕 提交于 2019-12-25 01:48:48
问题 I think that I copied the following dist function from Applicative Programming with Effects: The paper prefaces this function with: Have you noticed that sequence and transpose now look rather alike? The details that distinguish the two programs are inferred by the compiler from their types. Both are instances of the applicative distributor for lists dist :: Applicative f => [f a] -> f [a] dist [] = [[]] dist (v : vs) = [(:) v (dist vs)] However, I get the following compile-time error: ghci>