ghci

Haskell statements in GHCi being interrupted seems to break cmd

爱⌒轻易说出口 提交于 2019-12-10 20:19:06
问题 I'm just beginning with Haskell and playing around with GHCi in cmd. After trying out something I saw on PPCG, I ran into an issue. Whenever I interrupt fix with control-C, everything dies: C:\Users\Scrooble>ghci GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help Prelude> import Data.Function Prelude Data.Function> fix (\x -> x + 1) In^tCerrupte d. C:\Users\Scrooble>Prelude Data.Function> exit (hangs) The ^ and C were my keyboard interrupt. The exit is mine as well. Cerrupted is

In ghci, how to remove an existing binding?

99封情书 提交于 2019-12-10 17:31:38
问题 I am getting a "binding shadows the existing binding" error similar to the one from this question. Prelude Api.Facility Control.Monad.IO.Class> let t = getBadgesNot 1 (Nothing) (Just 1) <interactive>:55:5: warning: [-Wname-shadowing] This binding for ‘t’ shadows the existing binding defined at <interactive>:39:5 I defined the existing binding earlier in the session, and am now trying to redefine it. Is there a way to remove the existing binding so that I can redefine t ? I notice that in

What is going on when I compose “show” and “read” in Haskell?

若如初见. 提交于 2019-12-10 17:17:46
问题 Here's a short transcript from GHCi: Prelude> :t read read :: Read a => String -> a Prelude> :t show show :: Show a => a -> String Prelude> :t show.read show.read :: String -> String Prelude> (show.read) "whales" "*** Exception: Prelude.read: no parse When I compose show and read I can only assume that GHC chose some arbitrary type which is both Read able and Show able to be the "intermediate" type. How did it choose this type, and is there any way for me to find out what it is? 回答1: The GHCi

What is the difference between ++ and : in haskell?

烈酒焚心 提交于 2019-12-10 16:01:19
问题 I don't get this-- Prelude> "hi"++"there" "hithere" Prelude> "hi":"there" <interactive>:12:6: Couldn't match expected type `[Char]' with actual type `Char' Expected type: [[Char]] Actual type: [Char] In the second argument of `(:)', namely `"there"' In the expression: "hi" : "there" Prelude> Why doesn't that also return "hithere"? 回答1: The types. Try this in GCHi: Prelude> :t (:) (:) :: a -> [a] -> [a] Prelude. :t (++) (++) :: [a] -> [a] -> [a] 回答2: I get it now. The first operator needs to

Ambiguous type variable `a0' arising from a use of `it'

微笑、不失礼 提交于 2019-12-10 13:56:43
问题 I have the following function to return the Factor Pairs for a given number factorPairs:: (RealFrac a, Floating a, Integral a) => a -> [(a, a)] factorPairs n = map(\x -> (x, div n x)) [y | y <- [1..(ceiling $ sqrt n)], n `rem` y == 0] When I call the function in ghci factorPairs 18 I'm getting a run time error of * Ambiguous type variable `a0' arising from a use of `it' prevents the constraint `(Floating a0)' from being solved. Probable fix: use a type annotation to specify what `a0' should

Haskell's type inference strangeness

扶醉桌前 提交于 2019-12-10 04:35:47
问题 Look at this output from ghci: Prelude> :t Data.Map.lookup Data.Map.lookup :: Ord k => k -> Data.Map.Map k a -> Maybe a Prelude> :t flip Data.Map.lookup flip Data.Map.lookup :: Ord a => Data.Map.Map a a1 -> a -> Maybe a1 Prelude> let look = flip Data.Map.lookup Loading package array-0.3.0.2 ... linking ... done. Loading package containers-0.4.0.0 ... linking ... done. Prelude> :t look look :: Data.Map.Map () a -> () -> Maybe a Why look 's inferred type differs from type of flip Data.Map

Using ghci to find type

落花浮王杯 提交于 2019-12-10 04:01:57
问题 When I do something simple in ghci, like the following: let x = 7 + 2 I expect ghci to give a response of the type that x holds, like: x :: Integer When I run ghci, I do not get that the above line. How do I get that response? 回答1: To show types automatically use :set +t : μ> :set +t μ> let x = 7 + 2 x :: Integer μ> 回答2: Use the ghci :t command, like so: Prelude> let x = 7 + 2 Prelude> :t x x :: Integer Prelude> 回答3: To find the type of something in GHCi, you can use the :type command, or (as

Find max element and index of a list in Haskell

。_饼干妹妹 提交于 2019-12-10 03:26:34
问题 I'm taking my first steps into the wonderful world of Haskell. As an exercise, I would like to implement a method which finds the maximum element of a list and its index. Let's call this function "maxi". Calling maxi on a list should return the following result: ghci> maxi [1, 3, 4, 1, 2, 3] (4, 2) 4 is the largest int in this list, and it is located at index 2. I have attempted to implement this function as follows: maxim :: (Ord a) => [a] -> (a, Int) maxim l = let pmaxim :: (Ord a) => [a] -

How to abort execution in GHCI?

早过忘川 提交于 2019-12-10 01:52:27
问题 When I launch ghci> last [0..] I can interrupt it with Ctrl+C. However ghci> last (repeat 0) cannot be aborted with Ctrl+C. GHCI silently ignores the keystrokes. How to abort this command in GHCI? Is it a bug? 回答1: (Caveat lector: I use Linux, and run zsh on urxvt or gnome-terminal. If you use a different operating system, terminal, or shell, it's possible this will work differently for you.) The way I usually handle this is to hit Ctrl+Z (which puts it in the background, pausing execution

How can I stop infinite evaluation in GHCi?

℡╲_俬逩灬. 提交于 2019-12-09 14:21:45
问题 When I run something like: Prelude> cycle "ab" I can see an infinite printing of "ab". To stop it I just use Ctrl + c . And it works. When I run: Prelude Data.List> nub $ cycle "ab" I am not able to stop it. Question: Why it so? How can I stop this operation? Update: Ubuntu: version 18.10 GHCi: version 8.2.2 回答1: Great question! However, since How to abort execution in GHCI? already focuses on your second part, let's not repeat that here. Instead, let's focus on the first. Why it so? GHC