haskell

When are guard expressions appropriate?

Here is an example I wrote that uses if-else branches and guard expressions. When is one more appropriate over the other? The main reason I want to know this is because languages typically have a idiomatic way of doing things. test1 a b = if mod b 3 ≡ 0 then a + b else if mod b 5 ≡ 0 then a + b else a test2 a b | mod b...

What is the `isolate primitive` mentioned in the documentation of the "cereal" package?

There is an extensive collection of unique words in on the Haskell repository, cabal, (very slight exaggeration). Anyway today's term is isolate primitive. What is an isolate primitive? How does it compare to a non-isolate primitive? Unfortunately, I don't have the background to know most of the Haskell parlance, and Google isn't helping...

Haskell Tree help

Could anyone please help me with my assignment questions? I've done most of it but I'm still stuck on these 3 questions. Here is the question: Consider the following types of search trees and balanced trees data STree = Leaf | Node STree Int STree data Btree = Tip Int | Branch Btree Btree whose constructors are subject to ...

Learning Haskell: Seemingly Circular Program - Please help explain

I'm currently going through the book "The Haskell Road to Logic, Math, and Programming" by Doets and Van Eijck. I've never been exposed to any functional programming language until this book, so keep that in mind. Still early in the book, it gives the following code for a primality test: ldp :: Integer -> Integer ldp n = ldpf primes1 n...

How to optimize this haskell snippet

Hi guys! I'm trying to create a small module for doing decimal-based calculations. A number is stored as an integer mantisse, with a precision value specified by an int: data APNum = { getMantisse :: Integer , getPrecision :: Int } For instance: APNum 123 0 -> 123 APNum 123 1 -> 1.23 APNum 123 2 -> 12.3 ... (negative precision...

cross-compiling haskell code through ghc and mingw tools

I've tried -fvia-C and the -pgms, but none of them manage to create an executable, splurting out lots of errors like Warning: retaining unknown function ``L4' in output from C compiler. ...

classList in Haskell Help.

Hi guys, Can anyone help with my last assignment question that i got stuck on for 2 days straight. I'm suck at Haskell and this question seems to be too advanced for me, so please help me cos its due mid-night tonight. Any help is really appreciated. Here is my question. Your main task in this question is to define the function classl...

How to configure GHCi to automatically import modules

When I use GHCi, I almost always end up importing Control.Applicative, Data.List, etc. . Is there a way to configure GHCi to automatically import those modules. Also, after importing them, how do I keep the prompt from being insanely long? Prelude Control.Applicative Data.List Database.HDBC Database.HDBC.Sqlite3 System.Directory> ...

Multiple source files in Haskell

I'm writing my first big project in Haskell and I'd like to split it across multiple files. So far, I have written two modules, Parse and Eval. I'd like to have a Main module that just includes these two modules and specifies the main function. I have the files Main.hs, Parse.hs, and Eval.hs and import them in Main, but this happens: Pr...

Mutually recursive evaluator in Haskell

I'm writing an evaluator for a little expression language, but I'm stuck on the LetRec construct. This is the language: type Var = String type Binds = [(Var, Expr)] data Expr = Var Var | Lam Var Expr | App Expr Expr | Con Int | Sub Expr Expr | If Expr Expr Expr | Let Var Expr Ex...

parallel haskell (ghc 6.10.4) doesn't accept -N greater than -N1

I'm trying to run parallel code in GHC 6.10.4 (from MacPorts) on OS X 10.5 I'm building with -threaded, in my makefile: GHC=ghc -prof -auto-all -O -threaded glicko: glicko.hs Lib.hs $(GHC) --make -main-is Glicko glicko.hs lib.hs When I invoke ./glicko +RTS -N or ./glicko +RTS -N1 the code runs, but seems to only use one CPU...

Calculation steps in Haskell foldr

Does anybody know the steps of haskell 'foldr' use of function? GHCI Command Window: foldr (\x y -> 2*x + y) 4 [5,6,7] The result after evaluation: 40 Steps on this, Prelude> foldr (\x y -> 2*x + y) 4 [5,6,7] 6 * 2 + (7 * 2 + 4) 12 + 18 = 30 5 * 2 + 30 = 40 v ...

Haskell coding-style: map, fmap or <$> ?

Is there any reason to prefer one of the following notations over the others or is this simply a matter of preference? map toLower "FOO" fmap toLower "FOO" toLower <$> "FOO" As an aside: I realize that <$> is the same as `fmap`. Am I right in the assumption that map is just a less general form of fmap? ...

use Number as Monoid

Hi, I need to use a number (a Rational ) as the log type of a Writer Monad. Unfortunately , number are not Monoid. Is there a module (or something) instantiating Numbers as Monoid, or should I do it myself ? (I just want to know if it exists already somewhere to not reinvent the wheel, I don't need help to do it myself if needed) ...

Thread-ring benchmark

Today I was doing the thread ring exercise from the Programming Erlang book and googled for other solutions to compare. I found that the language shootout has exactly this the same problem as a benchmark. I had the impression that this is an area where Erlang should be fastest, but turns out that C and C++ are again on top. My suspicion ...

Haskell: Scope of variable when using lambda expression with bind functions

The following line works as expected, but I am a little concerned why: getLine >>= \x-> getLine >>= \y-> return [x, y] Consider the addition of parenthesis to scope the lambda expressions: getLine >>= (\x-> getLine) >>= (\y-> return [x, y]) The second line is errorneous because x is not in scope when used in the return, and I am ha...

Functor / Applicative instances for State in Haskell

After reading (and skimming some sections of) Wadler's paper on monads, I decided to work through the paper more closely, defining functor and applicative instances for each of the monads he describes. Using the type synonym type M a = State -> (a, State) type State = Int Wadler uses to define the state monad, I have the following (us...

Futures in Haskell

Does Haskell have an equivalent of Alice's ability to bind a variable to a future? val a = spawn foo; where foo is some function. I know Haskell supports channels and threads; I'm hoping for syntax as natural as Alice's to bind a value to a future and spawn a thread to calculate it without having to deal with the details. ...

Mixing Haskell and C++

Hi If you had the possibility of having an application that would use both Haskell and C++. What layers would you let Haskell-managed and what layers would you let C++-managed ? Has any one ever done such an association, (surely) ? (the Haskell site tells it's really easy because Haskell has a mode where it can be compiled in C by gcc...

Clojure replacement for ADTs and Pattern Matching?

Whenever in Haskell we need some variant data type, we would use ADTs in conjunction with pattern matching. What do Clojure folks use for such usecases? ...