haskell

Haskell Pattern Matching on Strings - Why doesn't this work?

getMultiWordPortion :: String -> String getMultiWordPortion (x:':':xs) = xs getMultiWordPortion _ = "" The expected result from pattern matching in this code is that everything before the first colon get's assigned to x, and everything afterwards gets assigned to xs, which is the result. If there is no colon, nothing is returned. What...

How to improve the performance of this Haskell program?

I'm working through the problems in Project Euler as a way of learning Haskell, and I find that my programs are a lot slower than a comparable C version, even when compiled. What can I do to speed up my Haskell programs? For example, my brute-force solution to Problem 14 is: import Data.Int import Data.Ord import Data.List searchTo = ...

What is the most elegant way to deal with an external library with internal state using a function programming language?

I'm currently playing around with Scala development, but I need to integrate with libraries such as box2d to handle physics. The problem is that this requires to depend on an external library which manages its own state. You keep track of the bodies which you pass into the box2d world. To sum up the aspects in play: Box2d manages the s...

"Instance of Integral Float required" error

The file with following function: type Point = (Float, Float) type Circle = (Float, Float, Float) getCircle :: Point -> Point -> Point -> Circle getCircle (a, b) (c, d) (e, f) = (x, y, r) where x = ((a^2+b^2)*(f-d) + (c^2+d^2)*(b-f) + (e^2+f^2)*(d-b)) `div` (a*(f-d)+c*(b-f)+e*(d-b)) `div` 2 y = ((a^2+b^2)*(e-c) + (c^...

How does Haskell exception handling work?

foldl1 (+) [] How do I catch the resulting error? ...

Graduate oppurtunities in functional programming/math area?

Hello, I'm currently applying to graduate schools, with the intention of getting a PhD in a field between math and CS. I am currently majoring in math, but two years ago I took a class in Haskell and got really interested in functional programming in general. I was interested in both the elegance of the ideas, both the abstract ones and...

Native newline characters?

What's the best way to determine the native newline characters such as '\n' or '\r\n' in Haskell? I see there is a "nativeNewline" function in GHC.IO:Handle, but assume that it is both a private API and most of all non-standard Haskell. ...

Comparing lists in Haskell, or more specifically what is lexicographical order?

Hi, I'm just beginning this nice hashkell beginners tutorial: http://learnyouahaskell.com on this page on lists he explains that lists are compared in compared in lexicographical order, he gives this example: ghci> [3,2,1] > [2,10,100] True From some googling it seems to me that lexicographical ordering means in alphabetical or seq...

Monads vs. Arrows

I'm broadly familiar with the concepts of monads and arrows as used in functional programming. I also understand that they can be used to solve similar kinds of problems. However - I'm still a bit confused about how to select which one to use in any given situation. When should I use monads and when should I use arrows? ...

How does one sort with Data.Vector.Generic.Mutable?

How can you sort a long list of data (strings, floats etc) that is read from a large file (say a few million lines) using a Data.Vector.Generic.Mutable object and a sort algorithm from Data.Vector.Algorithms? ...

Dynamic programming with Data.Vector

am using Data.Vector and am currently in need of computing the contents of a vector for use in computing a cryptographic hash(Sha1). I created the following code. dynamic :: a -> Int -> (Int -> Vector a -> a) -> Vector a dynamic e n f = let start = Data.Vector.replicate n e in step start 0 where step vector i = if i==n the...

Functional programming languages introspection

I'm sketching a design of something (machine learning of functions) that will preferably want a functional programming language, and also introspection, specifically the ability to examine the program's own code in some nicely tractable format, and preferably also the ability to get machine generated code compiled at runtime, and I'm won...

Which Haskell package for JSON

There are about a dozen JSON packages on Hackage for Haskell. How do I know which package I should use? How do I get a popular opinion? Are there any statistics on which package is being used the most, downloaded the most, etc.? ...

Haskell guards not being met

test :: [String] -> [String] test = foldr step [] where step x ys | elem x ys = x : ys | otherwise = ys I am trying to build a new list consisting of all the distinct strings being input. My test data is: test ["one", "one", "two", "two", "three"] expected result: ["one", "two", "three"] I am new to Has...

alternative to typeclasses?

haskell programmer. using F#. no typeclasses in F#. what to use when I need typeclasses? ...

Get the middle of an Ix range in O(1) time in Haskell

I was playing around with this code kata in Haskell, and I came across the question in the topic. It's trivial to find the midpoint of an array whose indexes are a single numerical value, but Haskell's array indexes can be any instance of the Ix typeclass, including, for example, the tuple (Int, Word, Card) where card is an instance of ...

GHC compilation error arising from import of Control.Exception

Continuing roadblocks when trying to learn Haskell. I am following the "Real World Haskell" and when it comes to getting one of their complex examples to work, I obtain the following error "Ambiguous type variable e' in the constraint: GHC.Exception.Exception e' arising from a use of `handle' at FoldDir.hs:88:14-61 Probabl...

Type classes in Scala

Hi stackoverflow. Having a background in Haskell I am currently trying to get familiar with Scala. I encountered some problems trying to translate a small, extensible expression language from Haskell into Scala. The underlying issue of writing a data type that is extensible with both new data-variants and operations is commonly known a...

If I have an expression, which is partitially evaluable, is it a good idea to avoid tail-recursion?

Consider an haskell-expression like the following: (Trivial example, don't tell me what the obvious way is! ;) toBits :: Integral a => a -> [Bool] toBits 0 = [] toBits n = x : toBits m where (m,y) = n `divMod` 2 x = y /= 0 Because this function is not tail-recursive, one could also write: toBits :: Integral a => a -> [Bool] toB...

How to make a Strategy in Haskell Control.Parallel.Strategies?

Update: I just found this documentation page. Wish there was a link to it from the documentation that I'd been using, which seemed to be the definitive API doc. But maybe it's a new, unreleased work. Update 2: This documentation has given me a lot better idea how to use the Control.Parallel.Strategies module. However I haven't quite sol...