haskell

Are monads Writer m and Either e categorically dual?

I noticed there is a dual relation between Writer m and Either e monads. If m is a monoid, then unit :: () -> m join :: (m,m) -> m can be used to form a monad: return is composition: a -> ((),a) -> (m,a) join is composition: (m,(m,a)) -> ((m,m),a) -> (m,a) The dual of () is Void (empty type), the dual of product is coproduct. Every...

How do I do automatic data serialization of data objects in Haskell

One of the huge benefits in languages that have some sort of reflection/introspecition is that objects can be automatically constructed from a variety of sources. For example in Java I can use the same objects for persisting to a db (with Hibernate) serializing to XML (with JAXB) or serializing to JSON (json-lib). You can do the same in...

Multiples of Numbers in a List

How would I print the multiples of a list of given numbers in a merged, sorted list? I.e. take 10 (multiples [4,5]) gives 4,5,8,10,12,15,16,20,24,25 I've got it working for lists of size 2 or 1 but I need a more general solution :) ...

Improve a haskell script

I'm a newbie in Haskell and I'd like some opinions about improving this script. This is a code generator and requires a command line argument to generate the sql script. ./GenCode "people name:string age:integer" Code: import Data.List import System.Environment (getArgs) create_table :: String -> String create_table str = "CREATE ...

Creating GUI desktop applications that call into either OCaml or Haskell -- Is it a fool's errand?

In both Haskell and OCaml, it's possible to call into the language from C programs. How feasible would it be to create Native applications for either Windows, Mac, or Linux which made extensive use of this technique? (I know that there are GUI libraries like wxHaskell, but suppose one wanted to just have a portion of your application l...

Parsec Haskell Lists

I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input and get a HTML output. If my input is: * First item, First level ** First item, Second level ** Second item, Second level * Second item, First level My output should be: <ul><li>First item, First level <ul><li>First item, Second level </li><li>...

Why isn't my IO executed in order?

Hi, I'm having some fun learning about the haskell IO. However in my recent exploration of it I have encountered some problems with IO not executing in order, even inside a do construct. In the following code I am just keeping track of what cards are left, where the card is a tuple of chars (one for suit and one for value) then the user ...

What is the relationship between ghc-pkg and cabal?

With respect to how packages are created, installed and used in Haskell, what is the relationship between ghc-pkg and cabal? What are their roles - when would you use one, over the other, or use both? Are they complementary tools, competitive tools, or simply tools that do different jobs? ...

Haskell Parse Paragraph and em element with Parsec

I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: this is the beginning of the paragraph --this is an emphasized text-- and this is the end\n And my output should be: <p>this is the beginning of the paragraph <em>this is an emphasized text</em> and this is the end\n</p> This code parses and returns a...

Generics and Constrained Polymorphism versus Subtyping

In this PDF presentation on Haskell Type Classes, slide #54 has this question: Open Question: In a language with generics and constrained polymorphism, do you need subtyping too? My questions are: How do generics and constrained polymorphism make subtyping unnecessary? If generics and constrained polymorphism make subty...

Literate Haskell (.lhs) and Haddock

At the moment I'm only using Haddock but after seeing some really interesting examples (e.g. this gist ) of literate Haskell I'm interested in trying it out in a project. The questions I got are: What do you write as Haddock comments and what do you write in the literate part? How do you scale literate programming to multiple files? C...

Image drawing library for Haskell?

I'm working on a Haskell program for playing spatial games: I have a graph of a bunch of "individuals" playing the Prisoner's Dilemma, but only with their immediate neighbors, and copying the strategies of the people who do best. I've reached a point where I need to draw an image of the world, and this is where I've hit problems. Two o...

GHC 6.12 and MacPorts

I recently installed (from the binary installers) GHC 6.12 and the Haskell Platform 2010.1.0.1 on my Intel MacBook running OS X 10.5.8, and initially, everything worked fine. Edit: I had to install cabal, alex, and happy from source, but after that, everything did seem to work fine. However, I discovered that if I use cabal install to ...

How do you install GHC into Cygwin or point Cygwin to GHC?

I attempted to point the Cygwin installer to http://haskell.org/ghc/cygwin, but the installer was unable to find setup.ini.sig. If possible, how could I alternatively edit my .bashrc to reference an installation made using the setup binaries in C:/ghc/. ...

How to use GD binding in Haskell?

I've installed the GD package (http://hackage.haskell.org/package/gd-3000.4.0) using cabal. When I try to compile the program which uses GD I receive an error: [mp262554@students:~/jpp/haskell]$ ghc gd.hs compilation IS NOT required gd.o: In function `sV8_info': (.text+0x1bf): undefined reference to `gdzm3000zi4zi0_GraphicsziGD_saveJpeg...

Strange pattern matching with functions instancing Show

So I'm writing a program which returns a procedure for some given arithmetic problem, so I wanted to instance a couple of functions to Show so that I can print the same expression I evaluate when I test. The trouble is that the given code matches (-) to the first line when it should fall to the second. {-# OPTIONS_GHC -XFlexibleInstance...

Level-order in Haskell

I have a structure for a tree and I want to print the tree by levels. data Tree a = Nd a [Tree a] deriving Show type Nd = String tree = Nd "a" [Nd "b" [Nd "c" [], Nd "g" [Nd "h" [], Nd "i" [], Nd "j" [], Nd "k" []]], ...

Haskell - Parsec Parsing <p> element

I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: This is the first paragraph example\n with two lines\n \n And this is the second paragraph\n And my output should be: <p>This is the first paragraph example\n with two lines\n</p> <p>And this is the second paragraph\n</p> I defined: line= d...

Haskell Parsec items numeration

I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: - First type A\n -- First type B\n - Second type A\n -- First type B\n --Second type B\n And my output should be: <h1>1 First type A\n</h1> <h2>1.1 First type B\n</h2> <h1>2 Second type A\n</h2> <h2>2.1 First type B\n</h2> <h2>2.2 Second ty...

Better exception for non-exhaustive patterns in case

Is there a way to get GHCi to produce better exception messages when it finds at runtime that a call has produced value that does not match the function's pattern matching? It currently gives the line numbers of the function which produced the non-exhaustive pattern match which though helpful at times does require a round of debugging w...