haskell

Haskell -- more type inference problems

I have the following expression: getCount :: (Num a) => a -> [a] getCount int = foldl processOneCount [0,0,0,0,0,0,0,0,0,0] (map (singleDigitCount) (map (digitToInt) (show int))) and i get the following error: Couldn't match expected type `a' against inferred type `Int' `a' is a rigid type variable bound by ...

What in the world does this vimscript do?

Full source here: http://www.vim.org/scripts/download_script.php?src_id=10391 The line is: silent %s/[^λ←→≲≳≡≠⇒»∙∀\\\-!#$%&*+/<=>?@\^|~.]\@<=\\\([^λ←→≲≳≡≠⇒»∙∀\\\-!#$%&*+/<=>\?@\^|~.]\)/λ\1/eg Someone please decipher this for me. The larger script is intended to unicode-ify some operators in Haskell into more familiar mathematical...

Non-negative integers

Say i have a function prototype as follows: func :: [Int] -> [Int] How is it possible to enforce only a non-negative list of integers as input arguments? I would have to change the param type from [Int] to what.. ? At this fair moment it works with func [-1,-2], i only want it to work with [1,2] i.e. with the interpreter spewing the er...

Haskell Type Error

Consider the following piece of Haskell code: type Parser a = String -> [(a, String)] item :: Parser Char item = \ inp -> case inp of [] -> [] (x:xs) -> [(x, xs)] ret :: a -> Parser a ret v = \ inp -> [(v, inp)] parse :: Parser a -> String -> [(a, String)] parse p inp = p inp pseq :: Parser ...

Pattern-matching Seq's in Haskell

Pattern matching is one of the most elegant Haskell features. I've been working on a project recently where I need a queue data structure so I'm using Data.Sequence. However, it looks like I have to give up the elegance of pattern matching and resort to guards: floodFillWorker :: Image -> RGBAColor -> Double -> PixelQueue -> Image fl...

Haskell Monad Transformer Stack and Type Signatures

I am attempting to create a stack of monad transformers and am having trouble getting the correct type signatures for my functions. (I'm still pretty new to Haskell) The stack combines multiple StateT transformers since I have multiple states I need to keep track of (two of which could be tupled, but I'll get to that in a second) and a ...

Which event-driven applications are implemented in Haskell?

I've been looking at Haskell lately and it seems like a very nice way to watch programming problems from an alternative point of view - alternative to my usual imperative (I have a strong C++ background) view, at least. However, all the articles I see seem to deal with the same kinds of programming problems: Parsers Compilers Numeric ...

Using Haskell to output a UTF-8-encoded ByteString

I'm going out of my mind trying to simply output UTF-8-encoded data to the console. I've managed to accomplish this using String, but now I'd like to do the same with ByteString. Is there a nice and fast way to do this? This is what I've got so far, and it's not working: import Prelude hiding (putStr) import Data.ByteString.Char8 (put...

Deriving a type and its dependencies

Hi, I've been playing with newtype wrappers for my indexes to avoid bugs, and I have some code like this: {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype PersonIdx = PersonIdx Int deriving (Enum, Eq, Integral, Num, Ord, Real, Show) To derive Integral, one must derive all its dependencies as well (hence the above list of type-cl...

Example of how to parse exiftool JSON output in Haskell

Completely stumped. Can't make sense of any of the documentation. Can someone please provide an example of how I can parse the following shortened exiftool output using the Haskell module Text.JSON? The data is generating using the command exiftool -G -j <files.jpg>. [{ "SourceFile": "DSC00690.JPG", "ExifTool:ExifToolVersion": 7.8...

Using Haskell's Parsec to parse a ByteString

I've managed to use Parsec to parse a String, but cannot manage to do the same with a ByteString. How can I make Parsec work with ByteStrings without manually converting them to Strings? I get the feeling this isn't hard to accomplish. Am I wrong? (I'm new to Haskell. ^^) Thanks! ...

Haskell - Some questions about System.Process and mutithreading

I have a small numerical simulation in C (I had to do it in C to share it with my advisor) but I want to use a "haskell script" like thing to organize the simulation. The program accepts some command line arguments and spits some output I'd like to redirect to a file, so I did something like this: import Control.Monad import System.Pr...

is there a way for keeping a variable from changing inside recursions, in haskell?

i'm trying to define the isInfixOf function, and i came to a solution(hope it works good :-)), but because of the recursion nature of haskell, i wrote another (help) function that almost duplicate the code of the function isInfixOf. this is the code: myIsInfixOf :: (Eq a) => [a] -> [a] -> Bool myIsInfixOf [] [] = True myIsInfixOf [] ...

Learning Haskell: How to remove an item from a List in Haskell

Trying to learn Haskell. I am trying to write a simple function to remove a number from a list without using built-in function (delete...I think). For the sake of simplicity, let's assume that the input parameter is an Integer and the list is an Integer list. Here is the code I have, Please tell me what's wrong with the following code a...

Haskell's Text.Regex.subRegex using TDFA implementation?

Is there a way to use the TDFA implementation of Regex with the functions in Text.Regex such as subRegex? The documentation says that Text.Regex must use the POSIX implementation only. ...

Unwrapping datatypes in Haskell without extraneous code

Say I have x = Just 2 Is there a way (preferrably a builtin mechanism/function) to use x in a single statement such that if it is a Just, then the 2 is automatically unwrapped and used, and if it is a Nothing, an exception is raised? That is, (f x) + 2 == 4 if x == Just 2, and raises an exception if x == Nothing. ...

please help me understand the pattern match in haskell. i'm a little confused.

if i have somthing like that: func (x1:x2:x3:xs) = xs then x1,x2,x3 must exist, yes? they can't be [], but must(again, MUST) be with a value, yes? also, the xs can be [] or [a] or [a,a,a] (etc'), yes? (in [a] i mean that it's a list with one number, and [a,a,a] is list of three numbers). also i have function that define isPrefixOf: m...

How do you use Control.Applicative to write cleaner Haskell?

In a recent answer to a style question, I wrote main = untilM (isCorrect 42) (read `liftM` getLine) and isCorrect num guess = case compare num guess of EQ -> putStrLn "You Win!" >> return True ... Martijn helpfully suggested alternatives: main = untilM (isCorrect 42) (read <$> getLine) EQ -> True <$ putStrLn "You Win!" ...

monad tranformers and stacking of multiple monads

I have a f function with signature f :: [a] -> StateT Int Reader b [c], and f' with signature f' :: a -> StateT Int Reader b [c] The computation in f (very simplified) looks like that: f [] = return [] f (s:st) = f' s >>= \x -> f st >>= \y -> return $ ... And in place of the ... I would like to return the [c] pa...

How to get system time in Haskell using Data.Time.Clock?

I'm needing some Ints to use as seed to random number generation and so I wanted to use the old trick of using the system time as seed. So I tried to use the Data.Time package and I managed to do the following: import Data.Time.Clock time = getCurrentTime >>= return . utctDayTime When I run time I get things like: Prelude Data.T...