haskell

[Haskell] Adding a Show instance to RWH's RandomState example.

Hi, I have just typed in the RandomState example from real world haskell. It looks like this: import System.Random import Control.Monad.State type RandomState a = State StdGen a getRandom :: Random a => RandomState a getRandom = get >>= \gen -> let (val, gen') = random gen in put gen' >> return val getTwoRandoms :: Random a =...

Concatenating lists in ternary trees

I'm making a function that concatenates ternary trees of lists, using an accumulator. 19 data Ttree t = Nil | Node3 t (Ttree t) (Ttree t) (Ttree t) 20 21 acc :: Ttree [a] -> [a] 22 acc tree = acc' tree [] 23 24 acc' :: Ttree [a] -> [a] -> [a] 25 acc' Nil rest = rest 26 acc' (Node3 xs l m r) rest = 27 xs $ acc' l $ acc' m ...

How to provide explicit type declarations for functions when using GHCi?

How to I define the equivalent of this function (taken from learnyouahaskell) inside GHCi? import Data.List numUniques :: (Eq a) => [a] -> Int numUniques = length . nub Without the type declaration, GHCi accepts the function definition, but it ends up with an unhelpful type: Prelude Data.List> import Data.List Prelude Data.Li...

Code Golf (sort of): a Functional Puzzle

Here's a puzzle: define some higher-order function f such that, using only f and parentheses, you can define the three higher-order functions const, id and bothOn. Those three functions could be defined in a straightforward way and in the Haskell programming language as follows: -- takes two arguments, ignores the second and returning ...

unique elements in a haskell list

okay, this is probably going to be in the prelude, but: is there a standard library function for finding the unique elements in a list? my (re)implementation, for clarification, is: has :: (Eq a) => [a] -> a -> Bool has [] _ = False has (x:xs) a | x == a = True | otherwise = has xs a unique :: (Eq a) => [a] -> [a] unique [] = []...

Haskell Custom Remove WhiteSpace

Hello to all, i have a and i would like to remove the white Space after "\n" and next character is white space. For instance, "username 123\n ugas 423\n peter 23\n asd234" become "username 123\nugas 423\npeter 23\nasd234" Please help. Thanks. ...

How do I get ghci to see packages I installed from cabal?

I've installed the such-and-such a package using cabal, and I can build a program that depends on it using cabal build. But when I load the same program in ghci, ghci complains that it "Could not find module `such-and-such'". I'm surprised this doesn't "just work." How do I tell ghci where to find the packages I've installed with cabal?...

Y-Combinator in FT EDSL

Hi all, I'm trying to figure out how to express the Y-Combitor in this Finally Tagless EDSL: class Symantics exp where lam :: (exp a -> exp b) -> exp (exp a -> exp b) app :: exp (exp a -> exp b) -> exp a -> exp b fix :: ... fix f = ..... I'm not certain but I think a default implementation of the Y-Combinator should ...

Categorizing GHC extensions

I was wondering if GHC's extensions can be divided into basically two different categories those that provide "syntactic suggar" or convenience and those that introduce something new, a new paradigm for instance. Now provided one could divide the existing extensions into the above categories which extension would fit into which categ...

Using Haskell to extend Perl?

Has anyone ever written a Haskell extension to Perl? Maybe something simple, like a function that calculates the fib. sequence? I'm interested in using Haskell, and I see some overlap between the Haskell and Perl community. Any pointers to Haskell / Perl projects, or cool things that manage to use both of these? I've seen Language::Haske...

Nice small simple real life programming example in Haskell?

Hi, I know, I might be asking much, but does any one know of some nice little programming examples / libraries for Haskell. For a 'introduction to programming' course I want to show that Haskell is more than a 'little script language to sort numbers'. I already found a Quake clone (Real world Haskell programming) which is impressiv...

Parser in Happy

I'm trying to do a parser with Happy (Haskell Tool) But I'm getting a message error: "unused ruled: 11 and unused terminals: 10" and I don't know what this means. In other hand I'm really not sure about the use of $i parameters in the statements of the rules, I think my error is because of that. If any can help me... ...

Are there any tutorials on building a simple interpreter using Alex + Happy?

I'm working on a school project where I have to build an interpreter for a simple language using Alex + Happy in Haskell. After looking through the documentation I understand most of it, but would like to see a full blown example on using the tools. ...

Haskell math performance on multiply-add operation

I'm writing a game in Haskell, and my current pass at the UI involves a lot of procedural generation of geometry. I am currently focused on identifying performance of one particular operation (C-ish pseudocode): Vec4f multiplier, addend; Vec4f vecList[]; for (int i = 0; i < count; i++) vecList[i] = vecList[i] * multiplier + addend; ...

Function Composition VS Function Application

Do anyone can give example of function composition? This is the definition of function composition operator? (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) This shows that it takes two functions and return a function but i remember someone has expressed the logic in english like boy is human -> ali is boy -> ali is hum...

Is Haskell truly pure (is any language that deals with input and output outside the system)?

After touching on Monads in respect to functional programming, does the feature actually make a language pure, or is it just another "get out of jail free card" for reasoning of computer systems in the real world, outside of blackboard maths? EDIT: This is not flame bait as someone as said in this post, but a genuine question that I a...

mysterious cabal-install problems

With a clean install of the "Haskell Platform". (OS X Snow-Leopard & Platform 2010.1.0.1), doing this causes simple sequence causes very weird cabal install behaviour: $ cabal install time $ cabal install random $ ghc-pkg list random /Library/Frameworks/GHC.framework/Versions/612/usr/lib/ghc-6.12.1/package.conf.d random-1.0.0.2 /Us...

Haskell testing workflow

I just started a new Haskell project and wanted to set up a good testing workflow from the beginning. It seems like Haskell has a lot of excellent and unique testing tools and many different ways to integrate them. I have looked into: HUnit QuickCheck benchpress HPC complexity Which all seem to work very well in their domains, but ...

Learning Haskell, care to help out?

I started learning haskell yesterday and I got stuck on a problem. After a bit of trying different things I thought I'd finally come here and ask how to fix this. Also, feel free to criticize the way I have done things so far so I can know what direction to go. Thanks. module Main where main = putStrLn lastPrime where lastPr...

Haskell Type Error From Function Application to Function Composition

This question is related to this http://stackoverflow.com/questions/3116165/function-composition-vs-function-application which answered by antal s-z. How you can get this ? map has type (a -> b) -> [a] -> [b] head has type [a] -> a map head has type [[a]] -> [a] Why the following code has type error for function composition ? t...