haskell

Fast element lookup for a functional language(Haskell)

Say we are traversing a graph and want to quickly determine if a node has been seen before or not. We have a few set preconditions. Nodes have been marked with integers values 1..N Graph is implemented with nodes having an adjacency list Every integer value from 1..N occurs in the graph, which is of size N Any ideas for doing this in...

New to functional programming

...

Homework: Doing a binary search on some elements in Haskell

Hello, I'm trying to complete the last part of my Haskell homework and I'm stuck, my code so far: data Entry = Entry (String, String) class Lexico a where (<!), (=!), (>!) :: a -> a -> Bool instance Lexico Entry where Entry (a,_) <! Entry (b,_) = a < b Entry (a,_) =! Entry (b,_) = a == b Entry (a,_) >! Entry (b,_) = a...

Haskell string list through lines

Hey, I'm using the lines functionality to take an input and split up many variables before sending it off to a function. Please look at the run function and tell me why I get the following error. It seems like it should just assign the first string in ln to seq, but I get an error. ERROR:dishonest.hs:33:11: Couldn't match expecte...

Haskell IO and closing files

When I open a file for reading in Haskell, I've found that I can't use the contents of the file after closing it. For example, this program will print the contents of a file: main = do inFile <- openFile "foo" ReadMode contents <- hGetContents inFile putStr contents hClose inFile I expected that interchan...

How much math do I need to become productive in Haskell?

I'm interested in learning Haskell on my own time (not through a course), but I suspect that my math background may be insufficient to grok important language concepts (TBD). I have had a year of college calculus (single variable), but not discrete math or logic. Am I going to run into gaps in my math? ...

Haskell on Windows Setup

Unfortunately I don't have access to a *nix box at work or at home. The only way I can play with Haskell is on windows. Anyone here using Haskell on Windows? What's your setup? ...

Haskell Typeclass shorthand

So, I have a pair of typeclasses that I'll be using a lot together, and I want to avoid specifying both each time. Basically, instead of putting :: (Ord a, Fractional a, Ord b, Fractional b, ... Ord z, Fractional z) => at the beginning of all my type specifications, I'd rather put :: (OrdFractional a, OrdFractional b, ... OrdFractio...

Haskell function definition and caching arrays

I have a question about implementing caching (memoization) using arrays in Haskell. The following pattern works: f = (fA !) where fA = listArray... But this does not (the speed of the program suggests that the array is getting recreated each call or something): f n = (fA ! n) where fA = listArray... Defining fA outside of a wh...

multiple type parameters in haskell type classes

I'm trying to do some abstraction in Haskell98 but doen't know how to do it. What I want to do is to define a class for types that may be converted into lists. toList :: a -> [b] But I don't know how to define a class for this method. I brought up the following three ideas: class ToList a b where toList :: a -> [b] class ToList...

How can I simplify a basic arithmetic expression?

How can I simplify a basic arithmetic expression? e.g. module ExprOps where simplify :: Expr -> Expr simplify (Plus(Var"x") (Const 0)) = Var "x" What do I have to do? ...

Algebraic types in haskell

How can I simplify an expression using basic arithmetic? ...

Rosetta Stone: reservoir random sampling algorithm

I've been killing some time with a little puzzle project -- writing the algorithm described in Knuth for random sampling of a population without knowing the size of the population in advance. So far I've written it in JavaScript Rhino, Clojure, Groovy, Python, and Haskell. The basic parameters are: the function takes two arguments, a s...

convert an exression to a string representation?

Consider the following Haskell code: module Expr where -- Variables are named by strings, assumed to be identifiers: type Variable = String -- Representation of expressions: data Expr = Const Integer | Var Variable | Plus Expr Expr | Minus Expr Expr | Mult Expr Expr d...

Primitive recursion

how will i define the function 'simplify' using primitive recursion? simplify :: Expr -> Expr ... simplify Simplify an expression using basic arithmetic, e.g. simplify (Plus (Var "x") (Const 0)) = Var "x" ...

Symbolic simplification in Haskell (using recursion?)

How can I give a general rule that includes all the expressions below? E.g one expression, another one for sub and one for mult. I need to use recursion but i got confused... simplify :: Expr->Expr simplify (Mult (Const 0)(Var"x")) = Const 0 simplify (Mult (Var "x") (Const 0)) = Const 0 simplify (Plus (Const 0) (Var "x")) = Var "x"...

How do I simplify the below expressions using primitive recursion ?

The simplifications I have in mind are 0*e = e*0 = 0 1*e = e*1 = 0+e = e+0 = e-0 = e and simplifying constant subexpressions, e.g. Plus (Const 1) (Const 2) would become Const 3. I would not expect variables (or variables and constants) to be concatenated: Var "st" is a distinct variable from Var "s". For example simplify(Plus (Var "...

Understanding the type error: "expected signature Int*Int->Int but got Int*Int->Int"

The comments on Steve Yegge's post about server-side Javascript started discussing the merits of type systems in languages and this comment describes: ... examples from H-M style systems where you can get things like: expected signature Int*Int->Int but got Int*Int->Int Can you give an example of a function definition (or two?) ...

How can I evaluate an expression?

How can I evaluate an expression, given a list of values for the variables it contains? eval::[(Variable,Integer)]->Expr->Integer Example: eval[("x",2), ("y",4)](Mult(Plus(Var "x") (Const))(Var "y"))= 12 ...

How can I write an addition of two items in a string representation?

For example I want to add two expressions e1 and e2 toString (Plus e1 e)= ?? I am guessing it would be something like toString (Plus e1 e)= ((toString e1) ++ "+" ++ (toString e2)) ...