haskell

what is programming paradigm in haskell

im preparing for my exam. i got this question and i read several articles about this. but still i cant get a proper idea about this question what Paradigm means is that different programming styles ( as far as i think) in the question they ask explain it by taking two paradigms so this explanation should be done using two styles of pr...

CPP extension and multiline literals in Haskell

Is it possible to use CPP extension on Haskell code which contains multiline string literals? Are there other conditional compilation techniques for Haskell? For example, let's take this code: -- If the next line is uncommented, the program does not compile. -- {-# LANGUAGE CPP #-} msg = "Hello\ \ Wor\ \ld!" main = putStrLn msg ...

Different results when applying function to equal values

I'm just digging a bit into Haskell and I started by trying to compute the Phi-Coefficient of two words in a text. However, I ran into some very strange behaviour that I cannot explain. After stripping everything down, I ended up with this code to reproduce the problem: let sumTup = (sumTuples∘concat) frequencyLists let sumFixTup = (13...

problem with Double and Rational Number

Hi, I am writing a function in which I need to read a string contains floating point number and turn it back to Rational. But When I do toRational (read input :: Double), it will not turn for eg: 0.9 into 9 % 10 as expected, but instead 81..... % 9007... Thx ...

Monads and custom traversal functions in Haskell

Given the following simple BST definition: data Tree x = Empty | Leaf x | Node x (Tree x) (Tree x) deriving (Show, Eq) inOrder :: Tree x -> [x] inOrder Empty = [] inOrder (Leaf x) = [x] inOrder (Node root left right) = inOrder left ++ [root] ++ inOrder right I'd like to write an in-order funct...

Very slow guards in my monadic random implementation (haskell)

Hi! I was tried to write one random number generator implementation, based on number class. I also add there Monad and MonadPlus instance. What mean "MonadPlus" and why I add this instance? Because of I want to use guards like here: -- test.hs -- import RandomMonad import Control.Monad import System.Random x = Rand (randomR ...

Pattern matching for custom read function

Hi, I am writing a custom read function for one of the data types in my module. For eg, when I do read "(1 + 1)" :: Data, I want it to return Plus 1 1. My data declaration is data Data = Plus Int Int. Thanks ...

How do I get Mathematica to thread a 2-variable function over two lists, using functional programming techniques?

Lets say I have a function f[x_, y_], and two lists l1, l2. I'd like to evaluate f[x,y] for each pair x,y with x in l1 and y in l2, and I'd like to do it without having to make all pairs of the form {l1[[i]],l2[[j]]}. Essentially, what I want is something like Map[Map[f[#1, #2]&, l1],l2] where #1 takes values from l1 and #2 takes value...

Higher-kinded Types with C++

This question is for the people who know both Haskell (or any other functional language that supports Higher-kinded Types) and C++... Is it possible to model higher kinded types using C++ templates? If yes, then how? EDIT : From this presentation by Tony Morris: Higher-order Polymorphism : Languages such as Java and C# have first-o...

Understanding Haskell's filter

I understand that Haskell's filter is a high order function (meaning a function that takes another function as a parameter) that goes through a list checking which element fulfills certain boolean condition. I don't quite understand its definition: filter:: (a->Bool)->[a]->[a] filter p [] = [] filter p (x:y) | p x = x:filter p y ...

Using MonadPlus in FRP.Reactive.FieldTrip

I'm studying FRP at this moment through FieldTrip adaptor. And hit the problem with strange way of frames scheduling and integration. So now I'm trying to build own marker Event for aligning Behaviour stepping. So... flipflop :: Behavior String flipflop = stepper "none" (xflip 2) where xflip t0 = do t <- withTimeE_ (atTime ...

The reason for MonadState get and put?

I'm reading the Monads chapter in Real World Haskell (chapter 14). A function is defined as follows: type RandomState a = State StdGen a getRandom :: Random a => RandomState a getRandom = get >>= \gen -> let (val, gen')= random gen in put gen' >> return val I don't really understand the purpose of the get and put funct...

GHCi error: Not in scope

I'm trying to compile this function from Learn You a Haskell for Great Good. removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']] by placing it into a removeNonUpperCase.hs file. It compiles fine, but when passing the argument: ghci> removeNonUppercase "Hahaha! Ahahaha!" the compiler says: <interactive>:1:0: Not in ...

Haskell: What's the type of this function?

mifun s = foldr op 0 s where op x r = head x + r Is there a way to make ghci tell me? ...

Working on Peano Axioms in Agda and hit a bit of a sticking point

PA6 : ∀{m n} -> m ≡ n -> n ≡ m is the axiom I am trying to solve and support, I've tried using a cong (from the core library) but am having troubles with the cong constructor PA6 = cong gets me nowhere, I know for cong I am required to supply a refl for equality and a type, but I'm, not sure what type I'm supposed to supply. Ideas? ...

Understanding this matrix transposition function in Haskell

This matrix transposition function works, but I'm trying to understand its step by step execurtion and I don't get it. transpose:: [[a]]->[[a]] transpose ([]:_) = [] transpose x = (map head x) : transpose (map tail x) with transpose [[1,2,3],[4,5,6],[7,8,9]] it returns: [[1,4,7],[2,5,8],[3,6,9]] I don't get how the...

"Strictly positive" in Agda

I'm trying to encode some denotational semantics into Agda based on a program I wrote in Haskell. data Value = FunVal (Value -> Value) | PriVal Int | ConVal Id [Value] | Error String In Agda, the direct translation would be; data Value : Set where FunVal : (Value -> Value) -> Value PriVal : ℕ...

Haskell as REST server

I would like to try Haskell on a smallish project which should be well suited to it. I would like to use it as a backend to a small ajax application. Haskell backend should be able to do authentication (basic, form, whatever, ...), keep track of user session (not much data there except for username) and to dispatch request to handlers b...

Good functions and techniques for dealing with haskell tuples?

I've been doing a lot of work with tuples and lists of tuples recently and I've been wondering if I'm being sensible. Things feel awkward and clunky which for me signals that I'm doing something wrong. For example I've written three convenience functions for getting the first, second and third value in a tuple of 3 values. Is there a ...

Why this Either-monad code does not type check?

instance Monad (Either a) where return = Left fail = Right Left x >>= f = f x Right x >>= _ = Right x this code frag in 'baby.hs' caused the horrible compilation error: Prelude> :l baby [1 of 1] Compiling Main ( baby.hs, interpreted ) baby.hs:2:18: Couldn't match expected type `a1' against inferred typ...