haskell

How to make Haskell compute the correct polymorphic type?

I just realized how useful the little on-function can be. Ex: orderByLength = sortBy (compare `on` length) But unfortunately, the inferred types can be somewhat counter-intuitive. According to the very definition f `on` g = \x y -> f (g x) (g y) one could e.g. replace (==) `on` length with \x y -> (length x) == (length y) B...

How to use 'oneof' in quickCheck (Haskell)

I am trying to write a prop that changes a Sudoku and then checks if it's still valid. However, I am not sure how to use the "oneof"-function properly. Can you give me some hints, please? prop_candidates :: Sudoku -> Bool prop_candidates su = isSudoku newSu && isOkay newSu where newSu = update su aBlank aCandidate aCandi...

Short Circuiting sort

I understand that: head (map (2**) [1..999999]) Will only actually evaluate 2**1, and none of the rest, but the book I am reading says that: head (sort somelist) Will only need to find the smallest item in the list, because that is all that is used. How does this work? As far as I can tell, this would be impossible with the sorting...

Inferred type appears to detect an infinite loop, but what's really happening?

In Andrew Koenig's An anecdote about ML type inference, the author uses merge sort as a learning exercise for ML and is pleased to find an "incorrect" type inference: Much to my surprise, the compiler reported a type of 'a list -> int list In other words, this sort function accepts a list of any type at all and returns a list ...

What algorithm to use to solve this simple mathematical problem efficiently?

I am puzzled with the following simple problem: Given positive integers b, c, m where (b < m) is True it is to find a positive integer e such that (b**e % m == c) is True where ** is exponentiation (e.g. in Ruby, Python or ^ in some other languages) and % is modulo operation. What is the most effective algorithm (with the lowest big-...

Reliable cube root in Haskell

Hi I am doing question 62 at project euler and came up with the following to test whether a number is cubic: isInt x = x == fromInteger (round x) isCube x= isInt $ x**(1/3) But due to floating point error, it returns incorrect results: *Main> isCube (384^3) False Is there a way to implement a more reliable cube test? On a side-no...

Mapping the :edit command to a certain editor

I've got the Haskell interpreter running in Emacs. With this I've got the typical Emacs setup of 2 open windows, the source file I'm editing the Haskell interpreter Using the command :edit file in the interpreter it will open the file I tell it to, but it will open it in Notepad. Is there a way to map the :edit command to use Emac...

List construction in Haskell

Hi I have the following recursive function for project euler question no. 74: chain n | n `elem` xs = length xs | otherwise = (chain (sumFac n)) : xs fac n = foldl (*) 1 $ enumFromTo 1 n sumFac n = sum $ map fac $ decToList n Except I don't know the correct syntax to construct a list on chain n so that it builds up a list of xs a...

Doubly Linked List in a Purely Functional Programming Language

How does one go about doing doubly linked lists in a pure functional language? That is, something like Haskell where you're not in a Monad so you don't have mutation. Is it possible? (Singly linked list is obviously pretty easy). ...

Learning Haskell: list comprehensions in C#

The following code is in Haskell. How would I write similar function in C#? squareArea xs = [pi * r^2 | r <- xs] Just to clarify... above code is a function, that takes as input a list containing radius of circles. The expression calculates area of each of the circle in the input list. I know that in C#, I can achieve same result, by...

importing c++ data types to haskell with ffi

hi, I'm writing a haskell wrapper for a c++ library and as much as I can import functions from the library to my haskell program, I have no clue how to import c++ data types. For instance I have a function which takes as a parameter a video::E_DRIVER_TYPE EDT_OPENGL type defined in some.h file, and as I said before I know how to import...

Avoiding boilerplate when dealing with many unrelated types

I'm writing code that deals with values from Language.Exts.Annotated.Syntax, where a variety of types are defined that mirror the structure of a Haskell module: data Module l = ... data Decl l = ... data Exp t = ... -- etc I'd like to be able to write functions that walk these data structures and perform various transformations on the...

Implementing a factorisation method in Haskell

Hi I am doing question 266 at project euler and after a bit of searching, found this method of quickly finding the factors of a number. What you do is find all the permutations of the prime factors of a number, these are its factors. I already have a module to find the prime power factors of a number, eg: Main> primePowerFactors 196 [(...

implementing a per-digit counter using the list monad

So, I was looking at the question here, and built a rather ugly solution for the problem. While trying to clean it up, I started investigating list comprehensions and the list monad. What I decided to do was to implement a per-digit counter using the list monad. Given an input sequence of digits, [1, 2], I wanted to generate an output...

Haskell pixel drawing library linux

I wish to draw individual pixels on a screen in a window or something for real-time display in haskell. I'm just getting started with haskell (but not functional programming, and not graphics), so I'm trying to create some rudimentary graphics things with it. I have tried to use SDL, but the following code gives me a blank screen: imp...

How Functional language are different from the language implementation point of view.

There is the whole new paradigm of "functional programming", which needs a total change of thought patterns compared to procedural programming. It uses higher order functions, purity, monads, etc., which we don't usually see in imperative and object oriented languages. My question is how the implementation of these languages differs fr...

Haskell question: constraining data types to use show

Code: data Exp a = Const a | Eq (Exp a) (Exp a) I want the Const a to contain a value of type show so that i can print it later. So in C# i would write: class Const : Exp { IShow X; } class Eq : Exp { Exp X, Y; } How can i do that in Haskell? ...

Choosing among alternatives in a Haskell algebraic datatype

When type X is defined as: data X = X { sVal :: String } | I { iVal :: Int } | B { bVal :: Bool } and I want the Int inside an X value, if there is one, otherwise zero. returnInt :: X -> Int How can I determine which type of X the argument to returnInt is? ...

reinventing the wheels: Node.JS/Event-driven programming v.s. Functional Programming?

Now there's all the hype lately about Node.JS, an event driven framework using Javascript callbacks. To my limited understanding, its primary advantage seems to be that you don't have to wait step by step sequentially (for example, you can fetch the SQL results, while calling other functions too). So my question is: how is this differen...

Ambiguous type variable

Related to my earlier question on traversing data structures, I'm having a problem making my code generic when I use it along with the uniplate package. I'm dealing with the data structures in the Language.Exts.Annotated.Syntax module, which are all generic with a type parameter l. This l is the same throughout the tree. The kind of cod...