haskell

How do I sort this list ?

I have a list of lists. List<List<T>> li = { {a1,a2,a3 ... aN}, {b1,b2,b3 ... bN}, ... }; double foo(List<T> list) { // do something // e.g {1,2,3} // it = 1 + 2 + 3 return it; } Now I want to sort li in such a way that higher the foo(x) for a x higher it should appear in a sorted list. What is the best w...

Haskell - specifying kind in data declaration

In this declaration data Const a = Const Integer Haskell infers that Const is * -> *. Is it possible to make Const take a type constructor instead, so it will be (* -> *) -> *? Ideally, it should be a -> *, but there are no polymorphic kinds. This thread shows one solution - adding unused constructor. Has the situation changed since 2...

Haskell uppercase character map -- confusing error

As some of you may know, the type inference errors in Haskell can be cryptic at times. I was trying to write a function that mapped alphabetic characters to their uppercase version, and I came up with this: toUpper :: Char -> Char toUpper char = maybe " " (\a -> a) isValue where charMap = zip ['a' .. 'z'] ['A' .. 'Z'] isVa...

How to get every Nth element of an infinite list in Haskell?

More specifically, how do I generate a new list of every Nth element from an existing infinite list? E.g. if the list is [5, 3, 0, 1, 8, 0, 3, 4, 0, 93, 211, 0 ...] then getting every 3rd element would result in this list [0,0,0,0,0 ...] ...

Haskell learning exercise gives strange results

this is the question: "Write a function that computes the mean of a list, i.e. the sum of all elements in the list divided by its length. (You may need to use the fromIntegral function to convert the length of the list from an integer into a floating point number.)" first i tried this: mean :: [Double] -> Double mean [...

What programming task provided your breakthrough with monads?

In a recent blog post about a probability monad he'd written, Mark Dominus wrote, "So I feel like I've finally arrived, monadwise." My first monadic program was an awkward solution to Problem 32 from Project Euler using parsec and the Maybe monad. What were you working on when the light finally turned on for you? Provide at least a ske...

How to create infinitely repeating list in Haskell?

I'm a C# guy trying to teach myself Haskell from Erik Meijer's Channel 9 webcasts. I came across an interesting puzzle which involved skipping every 'n' elements of a list using zip and mod. every :: Int -> [a] -> [a] every _ [] = [] every n xs = [x | (x,i) <- zip xs [1..], i `mod` n == 0] I've been thinking it might be more efficient...

Is there a haskell function for dates?

Is there a function in haskell that will allow me to enter component of a date (like a string representation or day month year components) that I can get information from (like day of week, days in a month, etc.)? I've looked online and it looks like there are a lot of custom libraries, but I'm hoping there's one in the standard prelude...

Why does ghci say that 1.1 + 1.1 + 1.1 > 3.3 is True?

I've been going through a Haskell tutorial recently and noticed this behaviour when trying some simple Haskell expressions in the interactive ghci shell: Prelude> 1.1 + 1.1 == 2.2 True Prelude> 1.1 + 1.1 + 1.1 == 3.3 False Prelude> 1.1 + 1.1 + 1.1 > 3.3 True Prelude> 1.1 + 1.1 + 1.1 3.3000000000000003 Does anybody know why that is? ...

Waiting on two inputs in Haskell, simultaneously

The title may be a bit vague. This is what I mean: Say I have two ways of getting input to my program. The first is via the keyboard, using the function getLine which blocks until a line is read. The other is, say, via a TChan, where using readTChan chan will also result in a block until a value is present in the channel, whereupon it w...

Haskell type error using infered type

I created a data type to hold basic user information and loaded it into ghci. I then used ghci to look at the new data types type signature. I copied the type signature from ghci into the source file and tried to reload the file. Ghci threw an error. The code and error are below. My question is, why is this throwing an error. I used th...

Haskell type families and dummy arguments

Hi, I made a function similar to numpy's array. It converts lists to arrays, lists of lists to 2d arrays, etc. It works like this: ghci> arrFromNestedLists ["hello", "world"] :: Array (Int, (Int, ())) Char array ((0,(0,())),(1,(4,()))) [((0,(0,())),'h'),((0,(1,())),'e'),((0,(2,())),'l'),((0,(3,())),'l'),((0,(4,())),'o'),((1,(0,())),'w'...

Control.Monad.State found in multiple packages haskell

While evaluating the line "import Control.Monad.State" in a Haskell module, GHC gives me the following error: Could not find module `Control.Monad.State': it was found in multiple packages: monads-fd-0.0.0.1 mtl-1.1.0.2 Failed, modules loaded: none. How do I resolve this conflict? ...

using oneOf in haskell, errors in compiling

Hi, I'm a complete newbie at Haskell. I'm trying to compile this Haskell file I've downloaded but it's giving me some errors. No instance for (Text.Parsec.Prim.Stream s m Char) arising from a use of 'letter' at Parse.lhs:649:26-31 Possible fix: add an instance declaration for (Text.Parsec.Prim.Stream s m Char) In the first argume...

Any Haskell online contests or problem sets?

Hi, All, Do you know any online problem sets or contests that accept solutions in Haskell? I know only El Judge, but unfortunately it has some problems with ghc, so any Haskell problem is rejected with "memory limit" resolution. ...

exercise on haskell, type definition, and guards.

first question: Define a function that joins a list of lists together using a separator value. the type definition should be like that: intersperse :: a -> [[a]] -> [a] The separator should appear between elements of the list, but should not follow the last element. Your function should behave as follows: ghci> :load Intersperse [1 o...

Haskell -- How to split a number into a list for further processing?

I have an Int that i want to split into it's individual numbers which ideally would be contained in a list, which i can then process further. So i would like something like this: split 245 --will then get an list containing [2,4,5] Is anyone familiar with such a function? ...

Reasoning about performance in Haskell

The following two Haskell programs for computing the n'th term of the Fibonacci sequence have greatly different performance characteristics: fib1 n = case n of 0 -> 1 1 -> 1 x -> (fib1 (x-1)) + (fib1 (x-2)) fib2 n = fibArr !! n where fibArr = 1:1:[a + b | (a, b) <- zip fibArr (tail fibArr)] They are very close to math...

Haskell: Composing two error-raising functions

The problem I have been given says this: In a similar way to mapMaybe, define the function: composeMaybe :: (a->Maybe b) -> (b -> Maybe c) -> (a-> Maybe c) which composes two error-raising functions. The type Maybe a and the function mapMaybe are coded like this: data Maybe a = Nothing | Just a mapMaybe g Nothing...

Haskell counted list type

So, just for fun, I've been playing with a CountedList type in Haskell, using Peano numbers and smart constructors. Type-safe head and tail just seem really cool to me. And I think I've reached the limit of what I know how to do {-# LANGUAGE EmptyDataDecls #-} module CountedList ( Zero, Succ, CountedList, toList, ofList, empty,...