haskell

Sampling sequences of random numbers in Haskell

I need small lists of gaussian random numbers for a simulation and so I tried the following: import System.Random seed = 10101 gen = mkStdGen seed boxMuller mu sigma (r1,r2) = mu + sigma * sqrt (-2 * log r1) * cos (2 * pi * r2) This is just the Box-Muller algorithm - given r1, r2 uniform random numbers in the [0,1] interval it ret...

Applicative functors other than monads and ZipList?

Two well-known examples of applicatives are monads and ziplists. Are there any other examples? ...

What is the effect of type synonyms on instances of type classes? What does the TypeSynonymInstances pragma in GHC do?

I'm reading Real World Haskell Pg 151, and I've stared at the following passage for over an hour: Recall that String is a synonym for [Char], which in turn is the type [a] where Char is substituted for the type parameter a. According to Haskell 98's rules, we are not allowed to supply a type in place of a type parameter whe...

Learning Haskell: String manipulation question

How can I write a function in Haskell, that takes an input string in the format a1a2a3 and expands into a1a2a2a3a3a3. For example input string "code" would be expanded into "coodddeeee" ...

Can GHCi tell me the type of a local Haskell function?

Is it possible to query the ghci for the type it inferred for a function inside another function? ...

Haskell function does not terminate

Hello, I´ve written a function in Haskell that takes three points in the plane, and checks if they´re on a straight line, or make a right or left turn. Here´s the code: detDirection :: Point -> Point -> Point -> Direction detDirection a@(Point (x1, y1)) b@(Point (x2, y2)) c = if (collinear1 a b c) then Straight else let ...

Haskell way to do error checking of multiple items with abort

What is a good way for a Haskell function to check a number of different conditions and return an error message on a failure? In Python or similar language, it would be straightforward: if failure_1: return "test1 failed" if failure_2: return "test2 failed" ... if failure_n: return "testn failed" do_computation How do you...

In Haskell, is there a way to do IO in a function guard?

For example: newfile :: FilePath -> IO Bool newfile x | length x <= 0 = return False | doesFileExist x == True = return False | otherwise = return True Can this be made to work? ...

Tracking down errors in Haskell

How can I get more information about where a Haskell error has occurred? For example, yesterday I was working on a Haskell program that parses an input file, transforms the data and then prints out reporting information. At one point, I ran "main" and got back *** Prelude.read: parse error with no other information. Fortunately, I ...

Best way to call Haskell functions from within Java

I'm looking an efficient way of executing Haskell functions from within a Java program. I've considered the use of exec() to interact with GHC, but it seems like there should be a better method. ...

What is the C# equivalent of map function in Haskell

map function in Haskell has two input parameters. The first parameter is a function and second parameter is a list. The map function applies the function passed as input parameter to all the elements in the list and returns a new list. Is there a C# equivalent to this functionality? ...

Haskell sort funtion

Why does Haskell's sort of Data.List ignore the third digit? Prelude>sort ["1","200","234","30"] ["1","200","234","30"] EDIT: Sorry, I did not realized that were string. My fault. ...

In Haskell, is there a way to do "elem" on a constructor with an argument?

Consider the following: data A = A1 | A2 Integer x = [A1, A2 999] elem A1 x == True Is there a way to do the following test? elem (A2 _) x == True ...

Polyvariadic Functions in Haskell

After reading this article on writing polyvariadic functions in Haskell, I tried to write some of my own. At first I thought I'd try to generalize it - so I could have a function that returned variadic functions by collapsing arguments as given. {-# OPTIONS -fglasgow-exts #-} module Collapse where class Collapse a r | r -> a where co...

Skipping exceptions when using map in Haskell

Hi I have the following code to return the length of a cycle in a string: module Main where import Data.List detec ys n | 2*n > (length ys) = error "no cycle" | t == h = (2*n - n) | otherwise = detec ys (n+1) where t = ys !! n h = if n == 0 then ys !! 1 else ys !! (n*2...

Why does Haskell stop short of inferring the datatype's typeclasses in the function signatures?

Firstly, this question isn't 100% specific to Haskell, feel free to comment on the general design of typeclasses, interfaces and types. I'm reading LYAH - creating types and typeclasses The following is the passage that I'm looking for more information on: Data (Ord k) => Map k v = ... However, it's a very strong convention in...

Haskell Ambiguous Occurrences -- how to avoid?

I do the following in GHCI: :m + Data.Map let map = fromList [(1, 2)] lookup 1 map GHCI knows that map is a (Map Integer Integer). So why does it claim an ambiguity between Prelude.lookup and Data.Map.lookup when the type is clear and can I avoid? <interactive>:1:0: Ambiguous occurrence `lookup' It could refer to either `Prel...

How to read bytes from a Socket in Haskell

I'm trying to read a number of Bytes from a socket in Haskell. Bascially I want to do something equivalent to this: client_socket.recv(255) #(Python) What's the best way of doing it? ...

Haskell tool to rewrite import statements to name all imports?

While writing Haskell code, I often simply type import System.Environment import System.Directory Is there a tool available that will amend these import statements and add the function names I actually use back into the import statements? Such as: import System.Environment (getArgs) import System.Directory (getDirectoryContents) Th...

haskell matrix scalar multilple question

hi I have a question about haskell coding. Matrices One of many ways to define matrices in Haskell is the list of matrix rows, where a row is a list of double precision oating point numbers: type Matrix=[[Double]] Using this type you should define the following functions (definition of dim is given as an inspirational example). dim...