haskell

Why is this Haskell SDL line cyan when it should be white?

Below is some code to use SDL with Haskell to draw a diagonal line. I get a CYAN line when the RGB clearly should be white. This is on Ubuntu. Am I doing something wrong? import qualified Graphics.UI.SDL as SDL import qualified Graphics.UI.SDL.Primitives as SDLP main = do SDL.init [SDL.InitEverything] SDL.setVideoMode 640 480 3...

Why is [String] the inferred type when there are no lists visible in the code?

I have this test: testReadFile = runTestTT $ TestLabel "InteractionDomain.readFileContentsToList" (TestList [ (TestLabel "testing infrastructure: read test file" (TestList [ TestCase (withTempFileContainingText (\fileHandle -> assertEqual "contents as expected" "The dog barks at midnight." (do co...

Haskell library like SymPy?

I need to manipulate expressions like 1 + sqrt(3) and do basic arithmetic like addition, subtraction, and division. I'd like the result to be in some sort of canonical form so that it can be used as a key in a map. Turning 1 + sqrt(3) into a float is not feasible due to roundoff problems. I used SymPy for this task in Python. Is there a...

Why doesn't the "$" function work consistently?

These lines execute correctly: Prelude> 1 / (1 + 1) 0.5 Prelude> (/) 1 $ (+) 1 1 0.5 Prelude> (/) 1 $ 1 + 1 0.5 This one does not: Prelude> 1 / $ (+) 1 1 <interactive>:1:4: parse error on input `$' Why? ...

Function computes the mean of a list - Better solution ?

Hello everyone, I've designed a function to compute the mean of a list. Although it works fine, but I think it may not be the best solution due to it takes two functions rather than one. Is it possible to do this job done with only one recursive function ? calcMeanList (x:xs) = doCalcMeanList (x:xs) 0 0 doCalcMeanList (x:xs) sum length...

Writing a function with a universally-quantified return type

If I write foo :: (Num a) => a foo = 42 GHC happily accepts it, but if I write bar :: (Num a) => a bar = (42 :: Int) it tells me that the expected type a doesn't match the inferred type Int. I don't quite understand why, since Int is an instance of the class Num that a stands for. I'm running into this same situation while trying...

how does one get strings (not string literals) into blazehtml attributes/element contents?

The blazehtml tutorial and other blog posts make it very clear how to get string literals to work. I've got that. But how do I get strings (bytestrings, Data.Text, etc. would work too.) in general into the attributes/content of elements. Blazehtml looks great, but it seems pretty useless without that. =P here's sample output and cod...

Inconsistent behaviour with Haskell

I was reading on perceptrons and trying to implement one in haskell. The algorithm seems to be working as far as I can test. I'm going to rewrite the code entirely at some point, but before doing so I thought of asking a few questions that have arosen while coding this. The neuron can be trained when returning the complete neuron. let n...

Transaction type safety

I have a Transaction monad that looks like: newtype Transaction t m a = .. my monad stack here .. t is a phantom type I use to make sure the transactions I chain up apply to the same backend store. My main loop's iteration uses a type like: Transaction DB IO (Widget (Transaction DB IO ())) The above type means: "A transaction that g...

Good, small Haskell compiler for linux?

I have SEVERE space restraints as far as linux goes, since I run linux off a 4GB flash drive. I know GHC is the preferred compiler for Haskell, but the GHC package is 280MB, which is way too big for me. Is there a smaller Haskell compiler for linux that works fine? ...

Haskell: Custom types with conditions

Hello everyone. I'm a haskell newbie and I couldn't find an answer to this question. Can we define types with conditions? For example, simple user-defined data type would be: data MyList = MyList [a] Can I somehow modify this code so MyList constructor can take only lists with even number of elements? Something like data MyList = ...

Haskell Bad Interface File

I am trying to take my Haskell project and split it apart into a library and a set of executables that depend on the library. When I try to compile now I get the following error: src/Main.hs:23:0: Bad interface file: /Users/<MyHomeDir>/.cabal/lib/Core-0.0.1/ghc-6.12.1/<MyModule>.hi mismatched interface file ways (wanted "", ...

how to execute haskell program in cygwin

I compiled my helloworld.hs and got a helloworld.o file, I tried ./helloworld, but it didn't work, so what is the right way to execute the helloworld? I am using cygwin, I just write down $ ghc --make helloworld.hs and I get helloworld.hi, helloworld.exe.manifest, helloworld.o files, I don't know what do I need to do next... ...

Haskell function composition (forward pipe) - why does this work?

In the below code, fibseq represents a sequence of numbers from the fibonacci sequence. (from code to solve Project Euler #2) I have defined an infix function |> as (|>) x y = y x. This lets me do the following (like a unix pipeline): take 34 fibseq |> filter even |> filter (< 4000000) |> sum My question is, why does this work? I wou...

Tutorial to disassemble the Haskell Cont monad?

This is how the Cont monad is defined: newtype Cont r a = Cont { runCont :: (a -> r) -> r } instance Monad (Cont r) where return a = Cont ($ a) m >>= k = Cont $ \c -> runCont m $ \a -> runCont (k a) c Is there a tutorial somewhere that picks this mumbo-jumbo apart and explains how and why this works? Not looking for a monad ...

Equivalent expressions produce different results in a function?

I have a function (exercise 10.11 in Thompson's The Craft of Functional Programming) which computes an approximation to the value of the definite integral of a function over a domain (a, b). It might not be the most elegant function, but I'm still a a beginner: import Data.Ratio (Rational, (%), denominator, numerator) type R = Rational ...

How do we keep multiple semantic values during parsing with Happy/Haskell

Hi, I'm trying to build a simple lexer/parser with Alex/Happy in Haskell, and I would like to keep some localisation information from the text file into my final AST. I managed to build a lexer using Alex that build a list of Tokens with localisation: data Token = Token AlexPosn Foo Bar lexer :: String -> [Token] in my Happy file, w...

'mod' and -1 in haskell

how come that following piece gives 4? (\x -> (x-1) `mod` 5) 0 ...

How to match newlines with Alex/Haskell

I borrowed the example presented here http://www.haskell.org/alex/doc/html/introduction.html I am trying to make an interpreter for numerical expressions. (literals only, no variables) And I want to have newlines separate two different expressions. Unfortunaltely, when I do this $ignoredWhite = [\t\f\v\r] -- ignored whitespace ...

Keep track of the program variables in Haskell like imperative programs

I'm having a hard time finding out how to make something change every time the user interacts with my program. It's hard to explain so here's an example (Haskell + wxhaskell): simulate :: Int -> Frame () -> IO () simulate qNr window = do fdata <- readFile "qarchive" case (split (listTake (split fdata '\n') qNr 0) '#') of (qst:a:b:c:...