haskell

Haskell 64 bit numerical type

I am writing a function in Haskell that deals with numbers beyond the length of a 32 bit int. I cannot find the type to do this and I seem to be searching for the wrong terms. It needs to be able to hold numbers with the length of about 2^40 without any loss of precision Example: addTwo :: Int -> Int -> Int addTwo a b = a + b main ::...

Pass Pointer to an Array in Haskell to a C Function

I have the following C code: #include <sys/times.h> #include <time.h> float etime_( float *tarray ) { struct tms buf; times( &buf ); tarray[0] = 1.0 * buf.tms_utime / CLOCKS_PER_SEC; tarray[1] = 1.0 * buf.tms_stime / CLOCKS_PER_SEC; return tarray[0] + tarray[1]; } Trying to port this Fortran code to Haskell: ...

haskell polymorphism and lists

Suppose I have the following: class Shape a where draw a :: a -> IO () data Rectangle = Rectangle Int Int instance Shape Rectangle where draw (Rectangle length width) = ... data Circle = Circle Int Int instance Shape Circle where draw (Circle center radius) = ... Is there any way for me to define a list of shapes, trav...

Is there a Haskell compiler or preprocessor that uses strict evaluation?

I'm looking for a Haskell compiler that uses strict evaluation by default instead of lazy evaluation. I would just use OCaml, but Haskell's syntax is so much better than OCaml's (and Haskell is pure, and has cool features such as type classes). I'd really rather not constantly put !s and $!s all over my program. A compiler with a switch...

Haskell FFI: Calling FunPtrs

Here's my situation: I would like to call ffmpeg's av_free_packet function: // avformat.h static inline void av_free_packet(AVPacket *pkt) { if (pkt && pkt->destruct) pkt->destruct(pkt); } But unfortunately this function is static inline, and so doesn't really appear in the linked library. However, it is a very simple function...

What are some problems best/worst solved by functional programming?

I've often heard that functional programming solves a lot of problems that are difficult in procedural/imperative programming. But I've also heard that it isn't great at some other problems that procedural programming is just naturally great at. Before I crack open my book on Haskell and dive into functional programming, I'd like at lea...

Translating "Why Functional Programming Matters" into Haskell

For cultural and intellectual enrichment, I've decided to learn a bit of Haskell. I've been reading Hughes' "Why Functional Programming Matters" and am trying to translate its code into true Haskell. I've attached some of my attempt below (for the numerical parts of the paper; the alpha-beta algorithm is even more interesting but I'd als...

[Haskell] case-insensitive regular expressions

What's the best way to use regular expressions with options (flags) in Haskell I use Text.Regex.PCRE The documentation lists a few interesting options like compCaseless, compUTF8, ... But I don't know how to use them with (=~) ...

How to learn Haskell

For a few days I've tried to wrap my head around the functional programming paradigm in Haskell. I've done this by reading tutorials and watching screencasts, but nothing really seems to stick. Now, in learning various imperative/OO languages (like C, Java, PHP), excercises have been a good way for me to go. But since I don't really know...

Haskell: type classes question

I wish to define the following typeclass Mapping: {-# LANGUAGE MultiParamTypeClasses #-} class Mapping k v m where empty :: m v insert :: k -> v -> m v -> m v search :: k -> m v -> Maybe v delete :: k -> m v -> m v One instance of Mapping is Data.Map.Map {-# LANGUAGE ..., FlexibleInstances #-} instance Ord k => Mapping k v ...

Haskell FFI: ForeignPtr seems not to get freed (maybe a GHC bug?)

Consider the following code snippet import qualified Foreign.Concurrent import Foreign.Ptr (nullPtr) main :: IO () main = do putStrLn "start" a <- Foreign.Concurrent.newForeignPtr nullPtr $ putStrLn "a was deleted" putStrLn "end" It produces the following output: start end I would had expected to see "a was deleted" some...

Haskell vs. procedural programming in the real world

These days I'm getting seriously into functional programming. While I'm really excited about Haskell and the possibilities it seems to offer, I can also see now that it is going to take me a while to learn. In an SO question on How to learn Haskell an answer states that it'll take months if not years to actually "master" it. Now, I kn...

Haskell foreign import stdcall on DLL function

This is probably a really easy question to answer, but for some reason I'm really struggling with it. I have a DLL written in C to access hardware at a protocol level, and I want to write a Haskell program that calls some of those C functions. Here's a snippet of the relevant C header (with names just slightly obfuscated due to possibl...

Multiline Matching in Haskell Posix

I can't seem to find decent documentation on haskell's POSIX implementation. Specifically the module Text.Regex.Posix. Can anyone point me in the right direction of using multiline matching on a string? A snippet for the curious: > extractToken body = body =~ "<textarea[^>]*id=\"wpTextbox1\"[^>]*>(.*)</textarea>" :: String I'm tryi...

Haskell: How do I get the values of #define-d constants?

In a Haskell program, what's the best way to use constants defined in C headers? ...

Generate Fortran 77 parser from a yacc grammar using Happy (Haskell)

I have stumbled upon the following F77 yacc grammar: http://yaxx.cvs.sourceforge.net/viewvc/yaxx/yaxx/fortran/fortran.y?revision=1.3&amp;view=markup. How can I make a Fortran 77 parser out of this file using Happy? Why is there some C?/C++? code in that .y file? UPDATE: Thank you for your replies! I've been playing with two fresh app...

Haskell lists of datatypes

This is probably another easy Haskell question. If I have some "nested" data types, such as in this example code: data Place = Country | State | City String deriving Show data State = California | NewYork deriving Show data Country = USA | Canada derivin...

How does 'get' actually /get/ the initial state in Haskell?

I have a function: test :: String -> State String String test x = get >>= \test -> let test' = x ++ test in put test' >> get >>= \test2 -> put (test2 ++ x) >> return "test" I can pretty much understand what goes on throughout this function, and I'm starting to get the hang of monads. What I don't understand is how...

How to move an element in a list in Haskell?

I'm reading through Learn You a Haskell and reached a spot where I'm trying to move an element in a list to the head. I've come up with what I think is the naive way and I'm curious if someone can show me what the experienced Haskell programmer would do instead. In this example, I have a list of Integers and I want to move the elemen...

How can I define an INI file grammar using the BNFC?

http://www.cs.chalmers.se/Cs/Research/Language-technology/BNFC/ how should I write my labeled BNF to get BNFC to generate a INI parser for me? I have only gotten so far o__O! entrypoints File ; comment "#" ; token ID ( letter | digit | ["-_'"] )+ ; Ini. File ::= [Section] ; Sect. Section ::= "[" ID "]" [Statement] ; Bind. Statement...