haskell

mtl, transformers, monads-fd, monadLib, and the paradox of choice

Hackage has several packages for monad transformers: mtl: Monad transformer library transformers: Concrete functor and monad transformers monads-fd: Monad classes, using functional dependencies monads-tf: Monad classes, using type families monadLib: A collection of monad transformers. mtl-tf: Monad transformer library using type famili...

Graph representation in Haskell

Hello, I have chosen to represent a graph in Haskell by a list of nodes (ex. n=[1,2,3,4]) and a list of pairs representing the edges (example m=[(1,2), (2,3)]). Now I have to see if the graph is strongly connected. My main issue is how to find if there is a way between 2 nodes in the graph. I wrote something like that: -- sees if 2 nod...

Sum of Fibonacci numbers

Hi, I'm rather new to Haskell. The problem is to find the sum of all even Fibonacci numbers not greater than 4 million. I can't use lists. If I understand correctly, the below solution is wrong, because it uses lists: my_sum = sum $ filter (odd) $ takeWhile (< 4000000) fibs Where fibs is the list of all Fibonacci numbers. Somehow,...

How do you solve this Haskell problem?

I want to define a function replicate to replicate a list of numbers by its value using only list comprehension, for example: replicate [5,1,3,2,8,1,2] output: [5,5,5,5,5,1,3,3,3,2,2,8,8,8,8,8,8,8,8,1,2,2] I know this would be easy to use the 'replicate' built in function but only list comprehension is allow, how can I do this? THAN...

How do I write a constant-space length function in Haskell?

The canonical implementation of length :: [a] -> Int is: length [] = 0 length (x:xs) = 1 + length xs which is very beautiful but suffers from stack overflow as it uses linear space. The tail-recursive version: length xs = length' xs 0 where length' [] n = n length' (x:xs) n = length xs (n + 1) doesn't suffer from this pr...

How do laziness and I/O work together in Haskell?

I'm trying to get a deeper understanding of laziness in Haskell. I was imagining the following snippet today: data Image = Image { name :: String, pixels :: String } image :: String -> IO Image image path = Image path <$> readFile path The appeal here is that I could simply create an Image instance and pass it around; if I need the ...

How do you solve this Haskell problem using higher order functions?

Using higher order functions (map, fold or filter) and if necessary lambda expressions. Write a definition for f1 and f2 so the following evaluation is valid: f1 (f2 (*) [1,2,3,4]) 5 == [5,10,15,20] Any help would be appreciated, thanks. ...

How do you solve this Haskell problem using a fold map and take?

Define a function replicate which given a list of numbers returns a list with each number duplicated its value. Use a fold, map, and take ..> replicate [5,1,3,2,8,1,2] output: [5,5,5,5,5,1,3,3,3,2,2,8,8,8,8,8,8,8,8,1,2,2] I've figure this out using List comprehension and recursion: replicate2 [] = [] replicate2 (n:nn) = take n(repe...

Haskell Target Architecture

I am trying to use the HDBC MySQL driver in my project. I seem to be having a problem related to target architecture of the processor. ld: warning: in /usr/local/mysql/lib/libmysqlclient.dylib, file was built for unsupported file format which is not the architecture being linked (i386) Since it can't read the file all the external refe...

Haskell : Type casting Int to String

I know you can convert a String to an number with read like so: Prelude> read "3" :: Int 3 Prelude> read "3" :: Double 3.0 But how do you grab the string representation of an Int value? ...

Control statements in Haskell?

I am just beginning Haskell, but from all the online tutorials I've found I can't seem to find if there is one accepted way to do a conditional control statement. I have seen if-else, guards, and pattern matching, but they all seem to accomplish the same thing. Is there one generally accepted/faster/more efficient way than the rest? ...

Why is the recursion idiom in Haskell "'n+1' and 'n'" and not "'n' and 'n-1'"?

I'm working my way through Graham Hutton's Haskell book, and in his recursion chapter, he often pattern-matches on "n+1", as in: myReplicate1 0 _ = [] myReplicate1 (n+1) x = x : myReplicate1 n x Why that and not the following, which (1) seems functionally identical and (2) more intuitive in terms of understanding what's happening with...

Haskell graph data type representation

I want to represent a graph in Haskell in the following manner: For each node I want to store it's value and a list of adjacent nodes,the problem which i'm having difficulties with is that I want the adjacent nodes to be stored as references to other nodes. For example: I want node ny to be stored as („NY“ (l p)) where l and p are adja...

Haskell: What is the difference between $ (dollar) and $! (dollar exclamation point)

Can anybody explain the difference in Haskell between the operators ($) and ($!) (dollar sign vs dollar sign exclamation point)? I haven't seen the use of $! anywhere so far, but while browsing through the Haskell reference on www.zvon.org, I noticed its existence and that it has the exact same definition as $. When trying some simple s...

haskell sorting

Hello, How can it be done in most simply way to write (or maybe there is something embedded in haskell) function which takse as arguments list of tuples (String, Int) and Int x and return top x tuples as list according to x value. I wonder if its possible to write a function which also takes 3 argument which is the name of (or index) ...

optional arguments in haskell

Hello, I have declared my own type: data Book = Bookinfo { bookId :: Int, title :: String } deriving(Show) and now: x = Bookinfo it is all ok, valid statement but making bookId x throws an error. If I would be able to handle errors in Haskell that would be ok but right now I cant do this So ...

Multiplying numbers on horizontal, vertial, and diagonal lines

I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit a stumbling block. One of the problem provides a 20x20 grid of numbers and asks for the greatest product of 4 numbers on a straight line. This line can be either horizontal, vertical, or diagonal. Using a procedural language I'd have no prob...

Implementing a very simple 'Wine Rating System' in Haskell

Hello, I have just started learning Haskell and have got stumped on how to add a rating to a custom data type. The data type I'm using has a name, a year and a tuple (userName and their rating), it looks like: data Wine = Wine String Int [Rating] deriving (Eq,Ord,Show,Read) type Rating = (String, Int) I wanted to allow a user to rate...

How to extract terms of specific data constructor from a list in Haskell

A common problem I got in Haskell is to extract all terms in a list belonging to a specific data constructor and I'm wondering if there are any better ways than the way I'm doing it at the moment. Let's say you got data Foo = Bar | Goo , the list foos = [Bar, Goo, Bar, Bar, Goo] and wish to extract all Goos from foos. At the momen...

installation HDBC-SQlite3 Haskell

Hello, Im facing the problem during installation : >>setup configure Configuring HDBC-sqlite3-2.3.0.0... setup: Missing dependency on a foreign library: * Missing C library: sqlite3 This problem can usually be solved by installing the system package that provides this library (you may need the "-dev" version). If the library is alread...