haskell

How to structure Haskell code for IO?

I'm trying to learn Haskell, so I decided to write a simple program to simulate the orbits of the planets around the sun, but I've run into a problem with printing out coordinates from the simulation, the top level function in my code is the following: runSim :: [Body] -> Integer -> Double -> [Body] runSim bodys 0 dtparam = bodys runS...

Generate vector code from Haskell?

Is it possible to get GHC to produce SIMD code for the various SSE generations? Eg. got a program like this import Data.Array.Vector main = print . sumU $ (enumFromToFracU 1 10000000 :: UArr Double) I can see the generated code (compiled for 64 bit x86) use SSE instructions in scalar mode (both C and asm backends). So addsd rather th...

Casting an mmapped ByteString to other types?

I realize this may be a rather heretical question, but I wonder whether I can mmap a file of data, via System.IO.Posix.MMap, and then cast the resulting ByteString into a strict array of some other type? Eg. if I know that the file contains doubles, can I somehow get this mmapped data into an UArr Double so I can do sumU etc on it, and h...

Is it better to use the State Monad, or to pass state recursively?

I'm just learning Haskell, and trying to figure out the most idiomatic way to implement a line of sight algorithm. The demo code I found uses the state monad, but it seem simpler to me (I'm just a beginner) to pass state recursively. What am I missing here? Are there performance problems? Find code at: http://www.finalcog.com/bresenh...

Haskell composition (.) vs F#'s pipe forward operator (|>)

In F#, use of the the pipe-forward operator (|>) is pretty common. However, in Haskell I've only ever seen function composition (.) being used. I understand that they are related, but is there a language reason that pipe-forward isn't used in Haskell or is it something else? ...

From OO to functional programming at 10,000 feet

I have been using f# and Haskell to learn functional programming for a while now. Until I can get f# approved at our company I must still use c#. I am still trying however to stay in the functional style as I have noticed several benefits. Here is a typical problem. There is a key-set table in the database with 3 keys (6.5 million row...

Instance of fractional [Char] required for definition?

This is simple code designed to take a decimal number and return a string representing the equivalent in binary. b2d :: Int -> String b2d 1 = "1" b2d x = show (x `mod` 2) ++ b2d x/2 However, when I try to run this through hugs, it gives me an error: :3 - Instance of fractional [Char] required for definition of b2d I don't know wh...

Libraries "rt" and "dl" in Cygwin

Perhaps this is stupid but I'm unable to find out which package I have to install in Cygwin to fix the following missing libraries: config.status: creating unix.buildinfo config.status: creating include/HsUnixConfig.h cabal.exe: Missing dependencies on foreign libraries: * Missing header file: HsUnix.h * Missing C libraries: rt, dl A...

Haskell function composition

I am reading this tutorial on Haskell. They define function composition as the following: (.) :: (b->c) -> (a->b) -> (a->c) f . g = \ x -> f (g x) No examples were provided, which I believe would enlighten me as to what is being defined here. Can someone provide a simple example (with explanation...

What uses have you found for higher-rank types in Haskell?

Higher rank types look like great fun. From the Haskell wikibook comes this example: foo :: (forall a. a -> a) -> (Char,Bool) foo f = (f 'c', f True) Now we can evaluate foo id without the compiler exploding. This example is quickly followed in the book by the real-world example I have seen in a few other places: the ST monad and runS...

How to specify type for bulk list assignments

I have the following code that looks like this [a,b,c,d] = ["a","b","c","d"] The compiler reports the warning: Warning: Definition but no type signature for 'a' Inferred type: a :: [Char] How to silence the warning and specify the type for this expression? ...

Python type inference for autocompletion

Is it possible to use Ocaml/Haskell algorithm of type inference to suggest better autocompletions for Python? The idea is to propose autocompletion for example in the following cases: class A: def m1(self): pass def m2(self): pass a = A() a. <--- suggest here 'm1' and 'm2' fun1(a) def fun1(b): b. <--- suggest here...

Making a list of divisors in Haskell

Hi I am doing problem 21 in eulerproject. One part requires finding the list of proper divisors of a number. i.e. where there is remainder of n and some number of less than n. So I made this Haskell, but GHCI gets angry at me. divisors n =[ n | n <- [1..(n-1)], n `rem` [1..(n-1)] ==0 ] The problem is that I don't know how to make: n ...

Haskell function taking a long time to process

Hi I am doing question 12 of project euler where I must find the first triangle number with 501 divisors. So I whipped up this with Haskell: divS n = [ x | x <- [1..(n)], n `rem` x == 0 ] tri n = (n* (n+1)) `div` 2 divL n = length (divS (tri n)) answer = [ x | x <- [100..] , 501 == (divL x)] The first function finds the divisors of ...

How to quickly produce a local datetime string in Haskell?

I'm looking for the best*est* way to produce the local time and date in string form, such as, for example: "2009-09-28-00-44-36.896200000000" I've been browsing Haskell's Data.Time docs, but I'm a bit stumped. (Big Haskell newbie here.) Thanks! ...

Detecting cyclic behaviour in Haskell

Hi I am doing yet another projecteuler question in Haskell, where I must find if the sum of the factorials of each digit in a number is equal to the original number. If not repeat the process until the original number is reached. The next part is to find the number of starting numbers below 1 million that have 60 non-repeating units. I ...

Call an haskell function in .net

Hi, I want to use an Haskell function with the following type :: string -> string from a C# programm. I want to use hs-dotnet to bridge both worlds, the author claim that it's possible, but provide no sample of this case. The only samples provided are the one to use .net from haskell. Does anyone got a sample of this use, or got an ide...

Something like mapM, but for arrays? (like arrayMap, but mapping an impure function)

Hello, I see that I can map a function over mutable arrays with mapArray, but there doesn't seem to be something like mapM (and mapM_). mapArray won't let me print its elements, for example: import Data.Array.Storable arr <- newArray (1,10) 42 :: IO -- answer to Life, Universe and Everything x <- readLn :: IO Int mapArray (putStrLn.s...

Problem using map with a list of lists in Haskell

Hi I am using Haskell to solve problem 99 in euler project, where I must find the maximum result from a list of base exponent pairs. I came up with this: prob99 = maximum $ map ((fst)^(snd)) numbers Where the numbers are in the form: numbers = [[519432,525806],[632382,518061],[78864,613712].. Why doesn't this work? Do I need to c...

Finding index of element in a list in Haskell?

I have a function in Haskell which finds the maximum value of an exponentiation from a list: prob99 = maximum $ map (\xs -> (head xs)^(head (tail xs))) numbers What I need to find is the location of this maximum value in the resultant list. How would I go about this? Edit: I found a solution that goes like this: n = [[519432,525806]...