a function that takes a (a -> a- > a) -> b function and an a as parameters and returns a b. This is correct. According to this reasoning , I should call this like : (<$>f) f 4 which would result in an Integer. No, because f does...

Provided by Thomas & Daniel in the comments, it turned out I was: Impatient. Large chunks eventually show up with the pi function, it's just a bit slow on my simple old coding machine. Not handling buffering in any way. So after rewriting the main function: import System.IO main ::...

Compiling with -Wall (or -Werror) is a good first start to finding the source of an uninitialized field. However, it might not be the source of the error. Missing record field initializions have a specific error message: Prelude> data A = A { a :: Int } deriving Show Prelude>...

In the final step, you mix up the argument oder. You unify b1 -> c1 with the type of map (:), but you should unify it with the type of foldr (++). More generally, you might want to learn how to debug such complicated calculations yourself. It is good that...

There's probably a better way to do this that's more cross-platform way that could be developed into a nice little package to load in a .ghci file or something, but the quick and dirty way using haskeline is pretty simple. You can just read in the ghci_history file for your...

Does the module you are compiling have a {-# LANGUAGE Safe #-} pragma at the top? Certainly this can't be followed by import Debug.Trace. If not, could it be that -XSafe is being used somewhere else, e.g. in the way you are calling ghci? Also, unSafeness is supposed to be...

It is not possible, basically because it breaks referential transparency: after "setting" the function the result of some other function will be different. (You might argue that undefined raising an exception is clear enough for everything to be safe, but then you need some way to make sure no function...

This looks like the effect of Monomorphism Restriction (introduced because otherwise there were incoherent things in the language, and the language designers couldn't come up with something less ugly): if something looks like a value rather than like a function (with arguments), it can't be polymorphic. (Your fstGt0 has no...

With the GHCi command :print ... (or :sprint ...) you can print out the variables in scope. This however will print only evaluated values (recall Haskell is lazy). To evaluate and print, just use the name of the variables directly. You can get a list of the variables in scope...

The line you are asking about is the function type signature, much like function prototype declaration in C. The specific line says that the function fac is taking an Int parameter and returning Int result. This is not always necessary in Haskell, since it can deduce types out of context...

GHC does not perform this optimization because it is not always an optimization space-wise. For instance, consider n = 1000000 x = (length $ map succ [1..n], length $ map pred [1..n]) On a lazy language such as Haskell, one would expect this to run in constant space. Indeed, the...

Define prompt in one of the config files Simply put that GHCi command in the appropriate GHCi config file (e.g. in your user-level GHCi config file $HOME/.ghci on Unix systems): :set prompt "ghci> " Create that file if it doesn't already exist. Then you won't have to run the command...

Does ghci not apply the default declaration to resolve type ambiguity? In GHC 7.8 or greater the Monomorphism Restriction is indeed off inside the GHCi shell, so declarations at the interactive shell will not apply the Num defaulting rules to top level expressions not under a lambda binder (...

Under any circumstances, which here implies: the function is never useful. This is actually a nice example of "theorems for free". Obviously, the type Ord t => [t] -> [a] doesn't make much sense, because the only list you can produce of a type [a] (you know nothing about a!)...

The problem is that GHCi is loading the Prelude package before even looking at your .ghci config. So you can fix the problem by running: ghci -XNoImplicitPrelude which you could alias to ghci in your shell. ...

One way is to use asTypeOf:: a -> a -> a. As a function, asTypeOf isn't very interesting, but its type is nice: it forces its two arguments and its return type to unify. So: > :t asTypeOf (undefined :: Int -> Bool) (undefined :: a -> Bool) asTypeOf (undefined...

There is no other way to completely avoid importing the Prelude. Even the seemingly-effective import Prelude () which is an explicit import (hence overrides the implicit one) and defines no names nevertheless puts a bunch of class instances in scope that may not be desired. Avoiding the standard prelude completely...

The identity function id :: a -> a, or explicitly \x -> x is polymorphic. This means it can be specialized to any type which you construct by substituting some type for a. In your case (>>= id) the compiler looks at the type of the second argument of (>>=)...

These operators are from Control.Applicative. You need to import Control.Applicative or say :m +Control.Applicative in ghci. You can find out where many standard operators come from using Hoogle. As of GHC 7.10, with the Functor-Applicative-Monad Proposal implemented in base 4.8, <*> is now present in the Prelude and does not...

The constraint a1 -> Bool ~ a2 -> Bool can be broken down to a1 ~ a2 and the obviously true Bool ~ Bool So you have a1 ~ a2. You already know that x is a1, xs is [a2] and, thanks to filter, the result type is [a2]. Therefore,...

You could try using typed holes if you have a new enough version of GHCi. Basically, if you write an underscore in the code, the compiler will spit out an error message telling you what type that subexpression should have. In your example, just replace "..." with "_"....

Since it is a literate script, you need to escape the pragma like other literate code: > {-# LANGUAGE KindSignatures, GADTs #-} or \begin{code} {-# LANGUAGE KindSignatures, GADTs #-} \end{code} dependent on which literate style you are using....

1 is a polymorphic value of type Num a => a. So in [1, [2, 3]], we have [2, 3] :: Num a => [a]; since all list elements must have the same type, we conclude that we must have 1 :: Num a => [a]. That's a bit weird...

You could run GHCi from your shell and use shell redirection. This works for me on Windows: echo :? | ghci > help.txt more help.txt I would expect this to also work on Unix, although I can't actually try it....

getAge Person "a" 1 Cabbage is parsed as (((getAge Person) "a") 1) Cabbage i.e. this would have to be a function accepting a Person-constructor and three more arguments, not a function accepting a single Person-value. Why it's done this way? Well, it makes multi-parameter functions much nicer. For instance, Person...

Perhaps you should set the encoding of the handle you're actually writing to. I don't know for sure, since I can't reproduce your problem, but something like this may do: withFile "asd.txt" WriteMode $ \h -> do hSetEncoding h utf8 hPutStr h "ćlččć" ...

By using three separate lets, you're defining three separate, non-exhaustive functions named max, each one shadowing the previous ones. In order to define a multi-case function using let, you use the let keyword ones and then just repeat the function signature at the same indentation for each pattern like this:...

The most common way of getting the visuals of that is using a font and a text editor that supports ligatures, like Hasklig. That way it looks like you want it to, but it's saved as a regular ASCII file, which makes it much easier to work with with tools...

All values must start with a lowercase character. If it starts with a capital or : then that value is a data constructor, to be used in data declarations. This is what you'll want to change your function to: lnat 0 = [0] lnat x = mod x 10 :...

This was GHC bug ticket #9033, reported in April 2014 and quickly fixed. Basically, whenever a file contained almost any type error, GHC would skip the type class defaulting step, which could cause other parts of the file to give spurious ambiguous type errors. As @leftaroundabout notes, the ^ operator...

Your first problem is the dreaded combination of the Monomorphism Restriction, GHCi's inability to see your whole program at once, and GHCi's extended defaulting rules. In a nutshell, Haskell doesn't like to infer types with polymorphic type class constraints (the Ord a => part of your type signature) for top-level...

Either use ghci -fobject-code -O Test.hs or cabal repl --ghc-options="-fobject-code -O". In more detail: ghci must be invoked with the -fobject-code flag. Optimization flag(s) must be given after -fobject-code on the command line, or in an OPTIONS_GHC pragma at the top of the module. Trying ghc --interactive -O -fobject-code produces...

Sure: λ> :{ | let result = | kakuro [ | (-2,0,0),(-2,0,0),(-2,0,0),(-2,0,0),(-2,0,0),(-2,0,0), | (-2,0,0),(-2,0,0),(-1,0,6),(-1,0,23) ... | -- AND SO ON... | ] | :} And then just evaluate result. However, I imagine that you've taken the list from the result of a previous command. In GHCi, the result of the...

Step 1 : declare an empty Int List... don't do that. Creating an empty thing an then filling it up is seldom a good idea. Sometimes, for performance reasons, it can be clever to create an uninitialised array (not empty, but "vacuous") and then define the contents after the...

You can use elem: > 'n' `elem` w True For future reference in case you haven't seen it before, there is actually a search engine that will look up functions based on type for Haskell. If I search for Char -> String -> Bool or String -> Char -> Bool...

This will not do what you think it does, in Haskell everything is immutable (once it has been given a value, that value cannot change). What you might be trying to do is: addNumbers :: Int -> IO [Int] addNumbers 0 = return [] addNumbers n = do print n...

If you are going to print something to the screen, your function has to be an IO action, so to start with it needs a type signature indicating that. Next, to perform a series of IO actions inside your function, you should use the do syntax, but this means to...

Sandboxes are a concept specific to Cabal, and runhaskell (and the rest of the GHC suite) has no idea about them. The relevant concept to GHC is the package database path, which can be controlled either by command-line flags like -package-db or by the environment variable GHC_PACKAGE_PATH. runhaskell seems not...

My guess, which wouldn't be visible from just the code, is that you have two versions of transformers installed: A newer one that is loaded directly by GHCi and an older one that is used by the installed either package. Or possibly even more packages are involved through indirect dependencies....

Just do what the compiler tells you to: add Eq constraint to your function. myCompare :: Eq a => Int -> Int -> [a] -> Int When you write a, you tell the compiler that the function can operate on any type; in other words, that means you don't expect...

type allows us to make synonyms, as you say. This means we can make shortened versions of long and complicated types. Here is the definition of the String base type. Yes, this is how it's defined: type String = [Char] This allows us to make types more readable when we...

The problem here is the conceptual interaction of odd and sin. You can't take the sin of a non-floating point, and a floating point can't be odd (or even, for that matter). GHCI tries to give you the benefit of the doubt for the purposes of exploration, so it doesn't...

Just so that we keep our answered questions ratio high on the Haskell tag, the solution was to use the :load or :l directives in GHCi to load the source file in the current directory. As @Zeta notes, the documentation with more details can be found at http://www.haskell.org/ghc/docs/latest/html/users_guide/interactive-evaluation.html#ghci-scope

maxBound is a function in the Bounded type class. By default, GHCi appears to choose the instance for (), which returns (). You can force it to use another instance by adding a type signature. let bInt :: Int; bInt = maxBound bInt -- 9223372036854775807 let x = maxBound x...

When you declare an algebraic data type, the right-hand side of the declaration is a "list" of possible constructors for this type. However, the constructors are just functions, meaning that they are used in prefix notation. You try to use the e.g. And constructor in an infix way which does...

Speaking more broadly, a Haskell file (or GHCi) does NOT contain a list of imperatives/orders, like some other programming languages. It is a different style of programming language. Instead there are a handful of kinds of top-level statements which you have access to: You can define values. y = y...

I investigated accelerate and accelerate-cuda and put some debug code to measure a time both under ghci and in a compiled, optimised version. Results are below, you can see stack trace and execution times. ghci run $ ghc -O2 -dynamic -c -threaded Main.hs && ghci GHCi, version 7.8.3: http://www.haskell.org/ghc/ :?...

Yes, when you load a file in GHCi like that, you can access anything defined in that file regardless of whether or not it is exported. This way expressions you write into GHCi behave exactly like they would inside the loaded file. So you can use GHCi to quickly test...

In both cases, they give the identity element of the operation: True && x == x False || x == x For each operation, it's the boolean that "does nothing", which makes it the perfect choice to return when you get nothing as an input! This is the same way...

The problem is with the indentation of your code. Use spaces for indentation. Indenting with 4 spaces is considered as a good practice. This code works perfectly: getNums = do putStrLn "enter a number (0 to terminate)" num <- getLine if read num == 0 then return [] else do...

You seem to have some confusion about the different string operators. A String is just a synonym for a list of chars i.e. [Char]. The colon : operator (aka cons) adds one element to the beginning of a list. Here's its type: *Main> :t (:) (:) :: a -> [a]...

In your first case, your type signature is wrong. It should be like this: bmiTell :: Double -> String -- Notice that there is no => In your second case, you are missing = in the last line. It should be like this: | otherwise = "huge" -- Notice the...

The problem is indicated by this part of the log file: /usr/bin/ld: cannot find -lgmp ghc can't find libgmp. Most likely fix is to install the libgmp-dev package, e.g. under Ubuntu: sudo apt-get install libgmp-dev ...

sub doesn't return [10,9,8,6,1] but [[10,9,8,6,1]] (a list of list) therefore the length is 1. You don't need the return. You are in a list monad, return wraps it's value into a list, this why you end up with nested list. Your code should be sub 5 = [1] --...

The flag you're looking for is -i<dir>: % ghci --help Usage: ghci [command-line-options-and-input-files] ... In addition, ghci accepts most of the command-line options that plain GHC does. Some of the options that are commonly used are: -i<dir> Search for imported modules in the directory <dir>. For example % ls src...