haskell

Haskell: print TextEncoding

Haskell newbie here. $ ghc --version The Glorious Glasgow Haskell Compilation System, version 6.12.1 While trying to debug weird locale-related bug in third-party Haskell program, I'm trying to print default encoding: import System.IO main = do print localeEncoding But it fails: $ ghc -o printlocale main.hs main.hs:4:2: No...

Why is this form of acceptable, but the other form raises a type error?

While working through Real World Haskell, I tried to complete the palindrome exercise using the following code solution: palin :: [a] -> [a] palin list = list ++ rev list where rev list | null list = [] | otherwise = rev (tail list) ++ (head list) Which raised a "cannot construct an infinite type error. Howev...

Haskell Pattern Matching on the Empty Set

I'm changing some Haskell code from using lists to sets. I understand everything required, I think, but I'm not sure how to pattern match on sets. Lists have this nice literal syntax that seems hard to emulate with the Set constructor. For example, I might have some code like this: foo [] = [] foo x = other_thing How can I write this ...

Int vs Integer in class instance

class Visible a where toString :: a -> String size :: a -> Int intToString :: (Integral t) => t -> String intToString 0 = "0" intToString 1 = "1" intToString 2 = "2" intToString 3 = "3" intToString 4 = "4" intToString 5 = "5" intToString 6 = "6" intToString 7 = "7" intToString 8 = "8" intToString 9 = "9" intToString n | ((div n 10...

SceneGraph traversal in Haskell

I want to implement a simple SceneGraph in Haskell using Data.Tree consisting of Transform and Shape nodes. In a SceneGraph the spatial transformation is accumulated while traversing and applied to the shape for rendering. type Transform = Vector2 Double data Shape = Circle Double | Square Double data SceneNode = XFormNode Transform...

Data.Vector.Binary overlaps Binary [a] instance

In my application I need to serialize a vector containing an arbitrary datatype, in this case is a list of Doubles. For serializing the vector I'm importing Data.Vector.Binary. When loading the module in GHCi the following error arises: Overlapping instances for Binary [Double] arising from a use of `decode' at Statistics.hs:57:33-42...

Problems decoding a file strictly with Binary in Haskell

Hi, I'm trying to read and decode a binary file strictly, which seems to work most of the time. But unfortunately in a few cases my Program fails with "too few bytes. Failed reading at byte position 1" I guess Binary its decode function thinks there is no data available, but I know there is and just rerunning the program works fine. ...

Haddock losing part of the documentation?

I have the following function: -- | Exponential moving average. ema :: (Fractional a) => a -- ^ Alpha -> [a] -- ^ Input data -> [a] -- ^ Output ema alpha xs = scanl1 (\a b -> a+(alpha*(b-a))) xs Haddock takes the above and generates the following HTML: ema :: Fractional a => a Input data -> [a] Output -> [a] Exp...

Haskell: Would "do" notation be useful for contexts other than monads?

We all love do, and I was curious if perhaps this sort of alternate syntax would theoretically be useful outside of the monad world. If so, what other sorts of computations would it simplify? Would it make sense to have something equivalent for Applicative, for example? ...

How to determine the directory in which a running Haskell script or application lives?

I have a Haskell script that runs via a shebang line making use of the runhaskell utility. E.g... #! /usr/bin/env runhaskell module Main where main = do { ... } Now, I'd like to be able to determine the directory in which that script resides from within the script, itself. So, if the script lives in /home/me/my-haskell-app/script.hs...

Automatic conversion of types for FFI calls in Haskell

I have defined the following module to help me with FFI function export: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, TypeSynonymInstances #-} module ExportFFI where import Foreign import Foreign.C class FFI basic ffitype | basic -> ffitype where toFFI :: basic -> IO ffitype fromFFI :: ffitype -> IO basic f...

Looking for a web host that can handle Haskell

I'm looking for a web host that will let me run a Haskell web application. VPS's seem attractive to me because you can run essentially anything you want. But some of the cloud hosts offer really nice scalability in terms of hard disk space and bandwidth. Does anyone know of a host that will let me run exotic languages like Haskell but...

[Haskell] Error: "No instances for (x)..."

Exercise 14.16-17 in Thompson asks me to add the operations of multiplication and (integer) division to the type Expr, which represents a simple language for arithmetic, then define the functions show and eval (evaluates an expression of type Expr) for Expr. My solution works for each arithmetic operation except division: data Expr = L...

Is mapM in Haskell strict? Why does this program get a stack overflow?

The following program terminates correctly: import System.Random randomList = mapM (\_->getStdRandom (randomR (0, 50000::Int))) [0..5000] main = do randomInts <- randomList print $ take 5 randomInts Running: $ runhaskell test.hs [26156,7258,29057,40002,26339] However, feeding it with an infinite list, the program never termin...

Haskell: read and type signatures

read is defined in the Prelude as read :: (Read a) => String -> a and can be used as e.g. read "1" :: Int. Now a function readOne :: (Read a) => [String] -> (a, [String]) readOne (x:xs) = (read x,xs) used with readOne ["1","foo"] results (as expected) in the error Ambiguous type variable 'a' in the constraint: 'Read a' arising...

Is Haskell suitable as a first language?

I have had previous exposure to imperative languages (C, some Java) however I would say I had no experience in programming. Therefore: treating me as a non-programmer, would Haskell be suitable as a first language? My interests in Pure Mathmatics and CS seem to align to the intention of most Haskell tutorials, and although i can inhere...

Why does the following simple Haskell function give slightly strange answers?

(%?) :: Int -> (Int -> Int) -> Int x %? f = f x m :: Int -> Int m v = v %? \z -> z * 2 %? \z -> z + 3 %? \x -> x + z or simpler p :: Int -> Int p v = v %? \z -> z * 2 %? \z -> z + 3 e.g, p 4 = 20 ...

Haskell in SPOJ: "Wrong answer" in problem, but correct in my computer

Hi I've been submitting solutions to the COINS problem in SPOJ today. I've got accepted the Fortran and the Perl solutions, but I'm having "Wrong answer" in the Haskell solution. I thought that perhaps my code was faulty at some point, but the results for 200K numbers (100K in the upper limit and 100K in the lower one) are exactly as pe...

Use of type synonyms in monad transformers

Is it possible to use type synonyms as arguments of monad transformers' type constructor? In particular, if there is an unary type synonym for an applied monad transformer, could it be used as a type of the underlying monad in another monad transformer? From what I see type synonyms are not accepted as first-class type constructors, see...

Stripping newlines in Haskell

What is the (or is there an) idiomatic way to strip newlines from strings in Haskell? Or do I have to craft my own by finding the trailing newlines/spaces and then removing them? EDIT: I'm looking for what Python's rstrip does, but don't need the optional "chars" argument: string.rstrip(s[, chars]) Return a copy of the string w...