ghci

What is going on with the types in this ghci session?

纵然是瞬间 提交于 2019-11-28 11:54:59
I'm learning Haskell, and I was playing around in ghci when I came across something very puzzling. First, create a simple add function: Prelude> let add x y = x + y Note that it works with ints and floats: Prelude> add 3 4 7 Prelude> add 2.5 1.3 3.8 Now create an apply function. It's identical to $ (but not infix). It works like a no-op on add: Prelude> let apply f x = f x Prelude> apply add 3 4 7 Prelude> apply add 2.5 1.3 3.8 Ok, now make add' which is the same as add' but using apply : Prelude> let add' = apply add Prelude> add' 3 4 7 Prelude> add' 2.5 1.3 <interactive>:1:9: No instance for

Why does ghci say that 1.1 + 1.1 + 1.1 > 3.3 is True?

喜你入骨 提交于 2019-11-28 11:17:04
I've been going through a Haskell tutorial recently and noticed this behaviour when trying some simple Haskell expressions in the interactive ghci shell: Prelude> 1.1 + 1.1 == 2.2 True Prelude> 1.1 + 1.1 + 1.1 == 3.3 False Prelude> 1.1 + 1.1 + 1.1 > 3.3 True Prelude> 1.1 + 1.1 + 1.1 3.3000000000000003 Does anybody know why that is? Brian Campbell Because 1.1 and 3.3 are floating point numbers . Decimal fractions, such as .1 or .3, are not exactly representable in a binary floating point number. .1 means 1/10. To represent that in binary, where each fractional digit represents 1/2 n (1/2, 1/4,

Debugging infinite loops in Haskell programs with GHCi

烂漫一生 提交于 2019-11-28 09:03:13
For the first time I've encountered an infinite loop in a Haskell program I'm writing. I've narrowed it down to a quite specific section of code, but I cannot seem to pinpoint exactly where I have a non-terminating recursive definition. I'm vaguely familiar with :trace and :history in GHCi, but the problem is that some branches of my code involve quite a bit of recursive modifications of a Data.Map.Map in the sense that the map x is obtained by adjust ing something in the map x' based on values in another map depending on x' . The specifics don't matter here, but as you can probably tell, if

Negative doubles or floats in Haskell (macports)

a 夏天 提交于 2019-11-28 08:40:43
问题 Why do I get a segmentation fault when I try to show a negative double or float? There is no problem for negative integers. Prelude> let a = 4 Prelude> :t a a :: Integer Prelude> let b = -4 Prelude> b -4 Prelude> :t b b :: Integer Prelude> let c = 5.6 Prelude> :t c c :: Double Prelude> let d = -5.6 Prelude> :t d d :: Double Prelude> show d "-Segmentation fault I tried it various ways, it seems that the number is correctly understood but not shown. Version info: ghci --version The Glorious

Haskell : loading ALL files in current directory path

萝らか妹 提交于 2019-11-28 08:16:03
问题 The command (in GHCi) :load abc Loads the functions in the file abc (which must exist in the current directory path). How would I load all the files in the current directory path? Thanks ---------------------------------------------------------------------------------- [RESPONSE TO POST BELOW] Hi Rotskoff, thanks I tried your suggestion but I could not get it to work, so I think I must have misunderstood something. I created 3 files test.hs, test1.hs, and test2.hs as follows : -> --test.hs

How to make a Haskell cabal project with library+executables that still run with runhaskell/ghci?

只谈情不闲聊 提交于 2019-11-28 03:06:05
If you declare a library + executable sections in a cabal file while avoiding double compilation of the library by putting the library into a hs-source-dirs directory, you cannot usually run your project with ghci and runhaskell anymore, especially if the executables have helper modules themselves. What is a recommended project layout that only builds what is needed once allows using runhaskell has a clean structure without hacks? nh2 Let's assume you have a mylib library, and mylib-commandline and mylib-server executables. You use hs-source-dirs for the library and each executable so that

How does GHCi pick names for type variables?

纵饮孤独 提交于 2019-11-27 23:38:38
问题 When using the interactive GHC interpreter, it's possible to ask for the inferred type of an expression: Prelude> :t map map :: (a -> b) -> [a] -> [b] It seems that it takes the names of the type variables from the signature since map is defined as map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs in the Prelude. That makes a lot of sense! My question is: how are type variable names picked when there is no signature given? An example would be Prelude> :t map fst map

GHCi “let” — what does it do?

折月煮酒 提交于 2019-11-27 19:42:45
I'd appreciate is someone could point to docs on what "let" does in GHCi, or failing that, explain it convincingly :-). So far as I can tell, "let" (without "in") is not part of the Haskell language per se, and on the other hand, it doesn't appear to be a GHCI command either, as it's not prefixed by colon. sinan While programming in GHCi, you're like programming in the IO monad with do syntax, so for example you can directly execute an IO action, or use monadic bind syntax like r <- someIOFun . let is also a part of do so you can also use this. I think it's being desugared into let .. in <rest

How to hack GHCi (or Hugs) so that it prints Unicode chars unescaped?

这一生的挚爱 提交于 2019-11-27 18:48:24
Look at the problem: Normally, in the interactive Haskell environment, non-Latin Unicode characters (that make a part of the results) are printed escaped, even if the locale allows such characters (as opposed to direct output through putStrLn , putChar which looks fine and readable)--the examples show GHCi and Hugs98: $ ghci GHCi, version 7.0.1: http://www.haskell.org/ghc/ :? for help Prelude> "hello: привет" "hello: \1087\1088\1080\1074\1077\1090" Prelude> 'Я' '\1071' Prelude> putStrLn "hello: привет" hello: привет Prelude> :q Leaving GHCi. $ hugs -98 __ __ __ __ ____ ___ ____________________

How to run a haskell file in interpreted mode

霸气de小男生 提交于 2019-11-27 18:04:55
I've been told you can interpret haskell files (which I assume means they will work like Ruby/Python/Perl). Can't find the command line option on ghc to do this, though. It always wants to compile my file. Took a look at ghci as well, but it always dumps me into a repl. I'm basically wanting to just do ghc -i MyFile.hs (where -i is a made up flag that I'm pretending correllates to interpreted mode) and have it execute so that I can get quick feedback while I'm trying out ideas and learning. $ runhaskell MyFile.hs Alternatively, runghc (they're the same thing). ghci MyFile.hs will also start an