haskell

Haskell Range Map library

Is there a Haskell library that allows me to have a Map from ranges to values? (Preferable somewhat efficient.) let myRangeMap = RangeMap [(range 1 3, "foo"),(range 2 7, "bar"),(range 9 12, "baz")] in rangeValues 2 ==> ["foo","bar"] ...

How do I use Haskell's type system to enforce correctness while still being able to pattern-match?

Let's say that I have an adt representing some kind of tree structure: data Tree = ANode (Maybe Tree) (Maybe Tree) AValType | BNode (Maybe Tree) (Maybe Tree) BValType | CNode (Maybe Tree) (Maybe Tree) CValType As far as I know there's no way of pattern matching against type constructors (or the matching functions i...

What is a simple way to wait for and then detect keypresses in Haskell?

I'm pretty new to Haskell, so I'm looking for a simple-ish way to detect keypresses, rather than using getLine. If anyone knows any libraries, or some trick to doing this, it would be great! And if there is a better place to ask this, please direct me there, I'd appreciate it. ...

Trouble calling a Haskell function from the main module

I've defined a function called findPaths in a Haskell Module called BinaryTree and I am trying to call that function in the main module I've created. The type of the function call is findPaths :: Tree -> [Path] Where Tree is a data type defined as: data Tree = Leaf | Node Tree Tree and Path is defined as: data Path = LeftTurn Path ...

How to write arithmetic expression to evaluate differentiation in haskell

I want the user to enter as Diff x^3 and get the answer as 3x^2 How can I write the arithmetic expression for this evaluation? What is the best way to do this in haskell? Thank you. ...

Haskell HXT for extracting a list of values

I'm trying to figure my way through HXT with XPath and arrows at the same time and I'm completely stuck on how to think through this problem. I've got the following HTML: <div> <div class="c1">a</div> <div class="c2">b</div> <div class="c3">123</div> <div class="c4">234</div> </div> which I've extracted into an HXT XmlTree. What I...

HXT -- what is "deep" ?

I'm putting in a lot of time trying to figure out how to use HXT. I keep coming against examples using deep. What does deep do? For example, this code has the following: atTag tag = deep (isElem >>> hasName tag) Another example: -- case-insensitive tag matching atTagCase tag = deep (isElem >>> hasNameWith ((== tag') . upper . localP...

Running Haskell HXT outside of IO?

All the examples I've seen so far using the Haskell XML toolkit, HXT, uses runX to execute the parser. runX runs inside the IO monad. Is there a way of using this XML parser outside of IO? Seems to be a pure operation to me, don't understand why I'm forced to be inside IO. ...

How do I maintain a server-side state with Snap Framework?

Server-side sessions are not [yet] part of the Snap Framework. Is there a way to add some sort of server side state? Let's pretend I want to increment a counter for each HTTP request. How would I do it? ...

Evaluation of a small math type language that supports one variable

I have written the parser that reads the string input. That works. I have also written the evaluator that spits out the result. But there is one small detail that I'm having trouble implementing. Look at the following example: +(sw+(2,2),sr) The sw construct of this tiny language is suppose to evaluate "+(2,2)" and store it somewhere...

modify edge label in Haskell package fgl

Hi, I've wrote the following code to increment the label of a given edge of a graph with FGL package, if the edge does not exist, it is created before being incremented : import Data.Graph.Inductive incrementEdge :: Edge -> Gr a Int -> Gr a Int incrementEdge edge g = gmap (increment edge) g increment :: Edge -> Context a Int -> ...

Library function to compose a function with itself n times

Is there a library function available in Haskell to compose a function with itself n times? For example I have this function: func :: a -> a and I want to do this: func . func . func . func . func . func , ... (up to n times, where n is only known at runtime). Note that the iterate function would not be appropriate for what I am...

Why is my code using monadic lists from the List package so slow?

Last week user Masse asked a question about recursively listing files in a directory in Haskell. My first thought was to try using monadic lists from the List package to avoid building the entire list in memory before the printing can start. I implemented this as follows: module Main where import Prelude hiding (filter) import Control...

Specifying package name for module-related commands in ghci

Is there a way to specify the package name for a module for the :browse, :load or :module commands in ghci (version 6.12.1) ? Some module names are ambiguous: Prelude> :module Control.Monad.Cont <no location info>: Ambiguous module name `Control.Monad.Cont': it was found in multiple packages: mtl-1.1.0.2 monads-fd-0.1.0.2 ...

Using Int type vs. Integral constraint. Advantage?

Consider these (more or less) equivalent type signatures: f1 :: [a] -> Int -> a f2 :: Integral b => [a] -> b -> a f2 is more general than f1, and that is a big advantage, but is there an advantage of f1 over f2? It seems that in the solutions to the H-99 problems, and in many posted solutions to various Project Euler problems, the f1...

Haskell: lift vs liftIO

In what situations should liftIO be used? When I'm using ErrorT String IO, the lift function works to lift IO actions into ErrorT, so liftIO seems superfluous. ...

haskell parsec optional question

Sorry if it's a novice question - I want to parse something defined by Exp ::= Mandatory_Part Optional_Part0 Optional_Part1 I thought I could do this: proc::Parser String proc = do { ;str<-parserMandatoryPart ;str0<-optional(parserOptionalPart0) --(1) ;str1<-optional(parserOptionalPart1) --(2) ;return str++str0+...

Filtering a list in Haskell

I am trying to start learning haskell, and a question came up. Say, I have a function countFilter :: (a -> Bool) -> [a] -> ([a], Int) countFilter a z = case z of [] -> ([], 0); (x:xs) -> (filter a z , length (filter a z)) It returns a list, all the items of which apply to a certain predicate and a...

Can anyone suggest a good constraint library for Haskell?

I've started learning about Constraint programming and I feel it is something that would work well with Haskell (also I enjoy using Haskell). Are there any mature constraint frameworks for Haskell? ...

Counting the number of recursions

Hi, I have some code that approximates a solution recusively, what it actually does is not important, but it works towards r' == rt by varying mg (m guess, starting with 4.0 because I "know" that ought to be in the ballpark). solve_m f ar st qt = solve_m' f ar st qt 4.0 where solve_m' f ar st qt mg | rd > precis = f' (...