haskell

How do i convert String to Integer/Float in Haskell

data GroceryItem = CartItem ItemName Price Quantity | StockItem ItemName Price Quantity makeGroceryItem :: String -> Float -> Int -> GroceryItem makeGroceryItem name price quantity = CartItem name price quantity I want to create a GroceryItem when using a String or [String] createGroceryItem :: [String] -> GroceryItem createGrocery...

Equivalent of python eval in Haskell

There is function in python called eval that takes string input and evaluates it. >>> x = 1 >>> print eval('x+1') 2 >>> print eval('12 + 32') 44 >>> What is Haskell equivalent of eval function? ...

Haskell Input & Passing Values to Functions

putStrLn "Enter the Artist Name" art <- getLine putStrLn "Enter the Number of CD's" num <- getLine let test= buyItem currentStockBase art num printListIO (showcurrentList test) the values i have to pass for buyItem is buyItem currentStockBase "Akon" 20 but i want to send "Akon" to art and for 20 i want to send num it ...

how to write a foldr HOF in Haskell

i want to add some amounts which in my tuple. i want to take that out and total it and show. i want to use Foldr function. can someone help me out how to do ...

How do I clear the terminal screen in Haskell?

How can I clear a terminal screen after my user has selected an option from my application's menu? ...

Enable --hyperlink-source for "cabal install"

The command cabal haddock has very useful --hyperlink-source option. I would like to have the source hyperlinked when building documentation with cabal install. The ticket #517 seems to be just about it: http://hackage.haskell.org/trac/hackage/ticket/517 However, perhaps it is possible to set this flag via ~/.cabal/config file? If not...

parsec-3.1.0 with custom token datatype

parsec-3.1.0 ( http://hackage.haskell.org/package/parsec-3.1.0 ) works with any token type. However there are combinators like Text.Parsec.Char.satisfy that are only defined for Char datatype. There doesn't seem to be any more general counterpart available. Should I define my own versions or did I miss something? Perhaps there are dif...

Haskell compile time function calculation

I would like to precalculate values for a function at compile-time. Example (real function is more complex, didn't try compiling): base = 10 mymodulus n = n `mod` base -- or substitute with a function that takes -- too much to compute at runtime printmodules 0 = [mymodulus 0] printmodules z = (mymodulus z):(...

Haskell: reading multiple command line arguments

Hi all, Okay, so I am making a program in Haskell that needs to change certain words based on two command line arguments. I have made the replace function and everything works great, but I am stumped getting it to work with command line arguments. Here is the main code: (replace function not included) main = do text <- getContents ...

Binding type variables that only occur in assertions

Hi! I find it extremely difficult to describe my problem, so here goes nothing: I have a bunch of assertions on the type of a function. These assertions rely on a type variable that is not used for any parameter of the function, but is only used for internal bindings. Whenever I use this function it does not compile because, of course, ...

How do you detect if a list has exactly 3 items in Haskell?

I have this but I got an error: -- test if a list contains exactly three characters test :: [Char] -> Bool test xs | [_ , _ , _] = True | otherwise = False ...

What is the difference between an Operator and a function in Haskell?

I am new to Haskell and this mixture of Infix and Prefix notation is confusing me. What is the difference between an operator like '+' and a function like head? How do I write an operator 'c' which does this 1 c 1 = 2? I found this definition a ! b = True. How does Haskell know that I am defining ! and not a function a? ...

Haskell Applicative and ErrorT?

Why is it that I can do the following: import Data.Word import Data.Binary.Get import Control.Applicative import Control.Monad.Error getW1 :: ErrorT String Get Word8 getW1 = lift getWord8 f1 = (+1) <$> getW1 but I cannot do: f2 = (+) <$> getW1 <*> getW1 and how I do I modify f2 so that it will work as I intend? ...

Haskell optimization of a function looking for a bytestring terminator

Profiling of some code showed that about 65% of the time I was inside the following code. What it does is use the Data.Binary.Get monad to walk through a bytestring looking for the terminator. If it detects 0xff, it checks if the next byte is 0x00. If it is, it drops the 0x00 and continues. If it is not 0x00, then it drops both bytes a...

Can Haskell's Parsec library be used to implement a recursive descent parser with backup?

I've been considering using Haskell's Parsec parsing library to parse a subset of Java as a recursive descent parser as an alternative to more traditional parser-generator solutions like Happy. Parsec seems very easy to use, and parse speed is definitely not a factor for me. I'm wondering, though, if it's possible to implement "backup" w...

Haskell: how to get through 'no instance for' ?

I am learning Haskell. I am on the 8th chapter of this book. The main thing I've learned so far is that Haskell is very unfriendly to me and it bites my ass where possible. Moreover... Heck! Enough mourning, to business. Here is the code: module GlobRegex ( globToRegex, matchesGlob ) where import Text.Regex.Posix import Text.Regex...

How do I write the function 'twice' in Haskell?

I want to write 'twice' function that takes a function and an argument and applies the function twice. However the function that it receives should work on union types. eg. f a -> b f b -> c Output twice f a c f a b f b c f c error eg. f :: Int -> String f :: String -> Char twice f :: Int -> Cha ...

Bubble sort algorithm implementations (Haskell vs. C)

Hello. I have written 2 implementation of bubble sort algorithm in C and Haskell. Haskell implementation: module Main where main = do contents <- readFile "./data" print "Data loaded. Sorting.." let newcontents = bubblesort contents writeFile "./data_new_ghc" newcontents print "Sorting done" bubblesort list = sort li...

What does a fullstop or period or dot (.) mean in Haskell?

I really wish that Google was better at searching for syntax: decades :: (RealFrac a) => a -> a -> [a] -> Array Int Int decades a b = hist (0,9) . map decade where decade x = floor ((x - a) * s) s = 10 / (b - a) ...

Haskell, list of natural number

Hello, I am an absolute newbie in Haskell yet trying to understand how it works. I want to write my own lazy list of integers such as [1,2,3,4,5...]. For list of ones I have written ones = 1 : ones and when tried, works fine: *Main> take 10 ones [1,1,1,1,1,1,1,1,1,1] How can I do the same for increasing integers ? I have tried t...