haskell

Why does this Haskell statement not evaluate lazily?

I have the following function defined: ex 1 x = 1 ex 0 x = 0 ex b x = b ** x Then, when I execute the following: 1 `ex` (sum [1..]) it tries to calculate the sum of the infinite sequence, instead of being lazy and returning 1. Why? EDIT: Upon further investigation, I found that laziness happens if I define the ex function in a ...

Haskell: How to write interactive interpreter on top of a State monad?

We're working on a model filesystem that uses a state monad internally. We have a type class with operations like these: class Monad m => FS m where isDirectory :: Path -> m Bool children :: Path -> m [Path] ... We're working on a little interactive interpreter that will offer commands like cd, ls, cat, and so on. An oper...

GHC refuses to export qualified modules

I want to write a module which re-exports some module it had imported qualified. Something like this: module Foo.A ( module Foo.B , module Foo.C ) where import qualified Foo.B import qualified Foo.C -- bunch of code using Foo.B and Foo.C here This looks like it ought to work; however, GHC prints warnings about the exports: Foo...

Memoization in Haskell?

Any pointers on how to solve efficiently the following function in Haskell, for large numbers (n > 108) f(n) = max(n,f(n/2)+f(n/3)+f(n/4)) I've seen examples of memoization in Haskell to solve fibonacci numbers, which involved computing (lazily) all the fibonacci numbers up to the required n. But in this case, for a given n, we only ne...

Help with debugging unexpected takeWhile behaviour with large numbers in Haskell

Firstly, apologies for the vague title, but I'm not sure exactly what I'm asking here(!). After encountering Haskell at university, I've recently started using it in anger and so am working through the Project Euler problems as an extended Hello World, really. I've encountered a bug in one of my answers that seems to suggest a misunder...

Haskell CPS Programming Style Problem

here is a function multiplies the elements in a list using CPS style mlist xx k = aux xx k where aux [] nk = nk 1 aux (0:xs) nk = k 0 aux (x:xs) nk = aux xs $ \v -> mul x v nk what if I change the 'k' to 'nk' in the expression aux (0:xs) nk = k 0, whats the difference between the two ? ...

How do I specify types for Haskell's set builder notation ?

Specifically, I want to do something like this : myfunc:: [(Integer, Integer)] myfunc = [(x^2 - y^2, 2 * x * y) | x <- [1..], y <- [1.. (x-1)]] When I try to load this through ghci I get Warning: Defaulting the following constraint(s) to type `Integer' `Integral t' arising from a use of `^' at myfunc.hs:76:20-22 So I ...

[Haskell] Pattern Matching and Recursion

I am new to both Haskell and programming. My question about binding in pattern-matched, recursive functions. For instance, suppose I have a function which checks whether a given list (x:xs) is a sublist of another list, (y:ys). My initial thought, following the examples in my textbook, was: sublist [] ys = True sublist xs [] = False sub...

How do I write, "if typeclass a, then a is also an instance of b by this definition."

I have a typeclass "MyClass", and there is a function in it which produces a string. I want to use this to imply an instance of Show, so that I can pass types implementing MyClass to "show". So far I have, class MyClass a where someFunc :: a -> a myShow :: a -> String instance MyClass a => Show a where show a = myShow a ...

Modifying the pretty printer from haskell-src-exts

The haskell-src-exts package has functions for pretty printing a Haskell AST. What I want to do is change its behavior on certain constructors, in my case the way SCC pragmas are printed. So everything else should be printed the default way, only SCCs are handled differently. Is it possible to do it without copying the source file and ed...

How can I factor this Haskell expression to avoid repeated computation?

I have this function (produces the fibonacci sequence): unfoldr (\(p1, p2) -> Just (p1+p2, (p1+p2, p1)) ) (0, 1) In here, I notice a repeated expression, p1+p2, which I would like to factor so that it is only calculated once. Addition itself isn't an expensive calculation, but for a more general version: unfoldr (\(p1, p2) -> Just (f...

Why this is not throwing any type error?

Hi I was trying some code segment from one of the blog and I came to notice the following code f :: Int -> [Int] f x = [1+x,2*x] test :: IO () test = putStrLn . show $ return 5 >>= f >>= f While executing I am getting [7,12,11,20]. Why the second 'f' function call is not throwing type error ? Is it something related to List Monad?...

Using Haskell's types to replace assert statements or if checks in other languages

Sorry if the question is very elementary, I am still very new to Haskell. Lets say I have a function that can only work with two numbers that are in the golden ration (1.618), how do I define the types of myfun x y to take only golden ratio numbers. What happens if I invoke myfun without golden ratio numbers from within my program (a com...

How do I get a callstack in Haskell?

I am trying to track down a non-exhaustive pattern in a libraries code. Specifically HDBC's mysql implementation. It is trying to match over types in my program and map them to mysql's types I believe. I can't seem to get a callstack for this error which means that since there are a number of parameters to the SQL query it is difficult t...

[Haskell] Odd values when enumerating a list

As part of a larger function definition, I needed to allow the domain (i, n) of a function to increment from i to n at varying rates. So I wrote: f (i, n) k = [i, (i+k)..n] into GHC. This returned odd results: *Main> f (0.0, 1.0) 0.1 [0.0,0.1,0.2,0.30000000000000004,0.4000000000000001,0.5000000000000001,0.6000000000000001,0.700000000...

newbie question: what role does the indentation play here? and Why one indent doesn't work?

Here is a sample program from RWH book. I'm wondering why the first works great but the second can't even compile? The only difference is the first one uses 2 tabs after where mainWith func = do whereas the second uses only 1. Not sure what difference does that mean? Why the second fails to compile? And also why do construct can be empt...

Cannot derive a Show instance for data containing UTCTime

I have data defined like the ff.: import Data.Time.Clock data D = D { ..., someDate :: UTCTime, ... } deriving (Eq, Show) When I compile it, I get the ff. error: No instance for (Show UTCTime) arising from the 'deriving' clause of a data type declaration at ... I already have the...

ghci displaying execution stack

So I'm working through some initial chapter exercise of Real World Haskell and I wanted to know if there is an option in GHCi to make it show function evaluation with parameters on each recursive call. So for example I wrote a simple version of 'map', and when I apply it, I would like GHCi to display each recursive call with actual argum...

How do typeclasses and modules interact?

Hi, In order to grasp better typeclasses (starting pretty much form scratch) I had a go at modelling 2-D shapes with area calculations, like this: module TwoDShapes where class TwoDShape s where area :: s -> Float data Circle = Circle Float deriving Show aCircle radius | radius < 0 = error "circle radius must be non-negative" ...

Haskell compile dll

Hello . I need to create dll for this module module MarketNews where import Foreign import Foreign.C.Types import Foreign.C.String import HighAPI(getNextNewsInfo) getNextNewsInfoM :: IO CString getNextNewsInfoM = getNextNewsInfo >>= \x -> newCString x foreign export stdcall getNextNewsInfoM :: IO CString I compiled : C:\Users\tes...