haskell

state monad haskell

I want to write a function for calculating the average using the State Monad in haskell this is the code I wrote as far import Control.Monad.State type MyState = (Double,Double) media s (a,n)= ((a*n+s)/(n+1),n+1) getAverage:: Double ->State MyState s1-> Double getAverage s c=get >>= \s0 -> let (x,s1) =media s s0 in put...

Operating on a return from a Maybe that contains "Just"

I have an algorithm that returns ->Maybe ([(Int,Int)],(Int,Int)) I would like to call this from another method and perform an operation on the data. However, the return value contains the keyword "Just" because it is a maybe. The second method takes ([(Int,Int)],(Int,Int)) and therefore will not except "Just ([(Int,Int)],(Int,Int))". ...

Tips for more elegant code with monads?

Hi. I finally got a hold on how to use monads (don't know if I understand them...), but my code is never very elegant. I guess is from a lack of grip on how all those functions on Control.Monad can really help. So I'd thought it would be nice to ask for tips on this in a particular piece of code using the state monad. The goal of the co...

renaming current module

Is it possible to rename current module ? Let's say I want to introduce data constructors Left and Right, but these would clash with Prelude.Left and Right. I don't want to write everywhere MyModule.Left and MyModule.Right, I would prefer to rename it somehow to MyModule as M and write M.Left. Is it possible ? What are the alternatives ?...

Haskell: nonobvious examples of functional dependencies

The examples of functional dependencies I've seen boil down to mapping container -> element, and arguments -> result (as in Mult Matrix Vector Vector). They seem to be better expressed with type functions. In database theory, more complex relationships are considered that are not of this form (like a -> b, b -> a). Are there examples of...

Memoization & Project Euler Problem 15 in Haskell

I've been learning some Haskell, and doing project Euler problems as I go. I'm not really bothered about the answer to the Euler Problem (which I can happily brute force, or do it in some other language), but the method. I'm stuck on doing Problem 15 in reasonable time. It asks for the number of non-backtracking routes from top-left to ...

Common recursion pattern

I'm getting used to Haskell's higher-order functions. Usually I can replace explicit patterns of recursion with functions like map, fold, and scan. However, I often run into the following recursion pattern which I don't understand how to express using higher-order functions: f (x:[]) = k x f (x:xs) = g x (f xs) For instance, sup...

Haskell or Ocaml with OpenGL and SDL precompiled distribution for Windows

I want to learn Ocaml or Haskell and I want to do it by writing a simple game. Apparently, there's one small problem: nobody cares about Windows and I want to do it on Windows, natively. Haskell has Cabal, which has SDL, but it doesn't build due to a trivial problem with no workarounds (order of parameters passed to gcc). Ocaml doesn't ...

Haskell Monad Join Function

While monads are represented in Haskell using the bind and return functions, they can also have another representation using the join function, such as discussed here. I know the type of this function is M(M(X))->M(X), but what does this actually do? ...

Haskell doubt: how to transform a Matrix represented as: [String] to a Matrix Represented as [[Int]] ?

Im trying to solve Problem 11 of Project Euler in haskell. I almost did it, but right now im stuck, i want to transform a Matrix represented as [String] to a Matrix represented as [[Int]]. I "drawed" the matrices: What i want: "08 02 22 97 38 15 00 40 [ ["08","02","22","97","38","15","00","40"], [[08,02...

Calculate n-ary Cartesian Product

Given two lists, I can produce a list of all permutations the Cartesian Product of these two lists: permute :: [a] -> [a] -> [[a]] permute xs ys = [ [x, y] | x <- xs, y <- ys ] Example> permute [1,2] [3,4] == [ [1,3], [1,4], [2,3], [2,4] ] How do I extend permute so that instead of taking two lists, it takes a list (length n) of list...

Efficiently finding multiple maximums in list of lists in Haskell

I am writing an algorithm for finding longs path over several turnpoints given a list of coordinates (that describe a path). The dynamic programming algorithm works nicely in O(kn^2), where k is the number of turnpoints and n number of points. To cut the story short: the slowest part is distance computation between 2 coordinates; the alg...

Duplicate definition for symbol __module_registered error

I get an error message from GHCi about a "duplicate definition for symbol __module_registered", like this: GHCi runtime linker: fatal error: I found a duplicate definition for symbol __module_registered whilst processing object file /usr/local/lib/ghc-6.2/HSfgl.o How to fix this? ...

rearranging string containing repeating patterns of variable length

I have a file with the following layout: TABLE name_of_table COLUMNS first_column 2nd_column [..] n-th_column VALUES 1st_value 2nd_value [...] n-th value VALUES yet_another_value ... go on ANOTHER TABLE repeat from begin..... I want to have this textfile rearranged for me, so I don't have to type in the TABLE an...

Have J style adverbs, forks etc been emulated via libraries in mainstream functional languages?

Has an emulation of J style of super condensed tacit programming via verbs, adverbs, forks, etc., ever been attempted via libraries for mainstream functional languages? If so, how successful was the result? If not, is there a technical issue that makes this impossible, or is it just not worth doing? I'm particularly interested in cons...

Purely functional soft heap

Are there any implementations of a purely functional soft heap data structure in any language? ...

In haskell, combining "case" and ">>="

I have a lot of code of the style: do x <- getSomething case x of this -> ... that -> ... other -> ... Any way of me combining the "x <- ..." and "case x of" lines to eliminate the need for a variable? ...

Prefix form of unary operator in Haskell

In GHCi: Prelude> (+3) 2 5 Prelude> (*3) 2 6 Prelude> (/3) 2 0.6666666666666666 Prelude> (-3) 2 No instance for (Num (t -> t1)) arising from the literal 3' at <interactive>:1:2 Possible fix: add an instance declaration for (Num (t -> t1)) In the expression: 3 In the expression: (- 3) 2 In the definit...

Haskell network package documentation

I want to experiment with network programming in Haskell. The problem I have is that the documentation for the network package is pretty scarce, especially the one for Network.Socket which I want to use. Do you know of some other references or clearly written projects where I can see how to use it? Are there any good alternatives to net...

How practical is it to embed the core of a language with an effectful function space (like ML) into Haskell?

As Moggi proposed 20 years ago, the effectful function space -> of languages like ML can be decomposed into the standard total function space => plus a strong monad T to capture effects. A -> B decomposes to A => (T B) Now, Haskell supports monads, including an IO monad that appears sufficient for the effects in ML, and it has a ...