Posts

Showing posts with the label haskell

How to check on nodes' content in Text.XML.Cursor?

Image
Clash Royale CLAN TAG #URR8PPP How to check on nodes' content in Text.XML.Cursor? I have this XPath works with xmllint //td[text() = 'USD']/../td[6]/text() Now I want it to be done in Haskell. But following (not complete of the original XPath) does not work at all. I got empty list error on head. cur $// element "td" &// check (c → "USD" ≡ head (c $// content)) By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

How to specialize mapM for IO in Haskell

Image
Clash Royale CLAN TAG #URR8PPP How to specialize mapM for IO in Haskell Say I have a task that represents some computation from k to v where some inputs have to be fetched externally. k v newtype Task k v = Task { run ∷ ∀ f. Monad f ⇒ (k → f v) → f v } For some tasks mapM will be used, e.g. to fetch multiple keys. I want to specialize mapM for some monads. Specifically for the IO monad I want to use Control.Concurrent.Async.mapConcurrently to perform actions concurrently. mapM mapM IO Control.Concurrent.Async.mapConcurrently My first instinct is to introduce a wrapper type newtype AsyncIO a = AsyncIO { io :: IO a } and then introduce instance Monad AsyncIO However this doesn't work because in the current GHC implementation mapM is defined in term of traverse , which is in Traversable . mapM traverse Traversable Is there an elegant solution for this? I don't quite understand what you mean by "for some tasks mapM will be used". Could ...

Compiling Pascal function return value assignment

Image
Clash Royale CLAN TAG #URR8PPP Compiling Pascal function return value assignment I'm writing a toy Pascal compiler in Haskell. I can produce a Type-annotated AST. I've been ignoring function return value assignments: function foo : integer; begin foo := 12 end; How do I handle those, given that the grammar doesn't distinguish between them and any other regular assignment statement? Should the AST be modified to specifically highlight a return value assignment? And how to deal with them in the type checking phase (An environment Symbol table consisting of function signatures and a stack of contexts was enough so far)? I think the type checking question is probably too broad, and the reason for the close vote. I think the AST question would be good if this question included things like the current AST data structure. – Carl 28 mins ago ...

Defining an Ordinal type whose enumerated subtypes are not ordinal

Image
Clash Royale CLAN TAG #URR8PPP Defining an Ordinal type whose enumerated subtypes are not ordinal I'm running into issues defining ordinal types whose values may or may not be ordinal. Basically I have two types, an OrderedType and an UnorderedType OrderedType UnorderedType data OrderedType = One | Two | Three deriving (Eq, Ord, Show) data UnorderedType = Red | Blue | Green deriving (Eq, Show) I have a type whose value constructors take either as an argument: data WrapperType = WrappedOne OrderedType | WrappedTwo UnorderedType deriving (Eq, Ord, Show) Basically, what I want to be able to do is have WrapperType s be ordered without having to implement a compare function for WrappedOne and WrappedTwo . WrapperType compare WrappedOne WrappedTwo When I try to compile the above I get the following error: • No instance for (Ord UnorderedType) arising from the first field of ‘WrappedTwo’ (type ‘UnorderedType’) Possible fix: use a standalone 'deriving instance...