ghci

Non exhaustive pattern in function in GHCi

夙愿已清 提交于 2019-11-26 06:08:23
问题 I want to make a function that displays the last element of a list. This is my code: ghci> let myLast :: [a] -> a ghci> let myLast [] = error ghci> let myLast [x] = x ghci> let myLast (x:xs) = myLast xs And I get the following error: ***Exception: Non-exhaustive patterns in function myLast I understood that you get this error when you\'re missing a case, but I think I have included all the possibilities. Any ideas? 回答1: If you use a let in each line, each definition will make a new function

Multi-line commands in GHCi

前提是你 提交于 2019-11-26 05:57:38
问题 I am having problem in entering multi-line commands in ghci. The following 2-line code works from a file: addTwo :: Int -> Int -> Int addTwo x y = x + y But when I enter in ghci, I get an error: <interactive>:1:1: error: Variable not in scope: addTwo :: Int -> Int -> Int I also tried putting the code inside :{ ... :} , but they are also not working for this example, because this is just appending the lines into one line, which should not be the case. I am using WinGHCi, version 2011.2.0.1 回答1

:sprint for polymorphic values?

纵然是瞬间 提交于 2019-11-26 04:55:27
问题 I am wondering why :sprint reports xs = _ in this case: Prelude> xs = map (+1) [1..10] Prelude> length xs 10 Prelude> :sprint xs xs = _ but not in this case: Prelude> xs = map (+1) [1..10] :: [Int] Prelude> length xs 10 Prelude> :sprint xs xs = [_,_,_,_,_,_,_,_,_,_] Note: I am running ghci with -XNoMonomorphismRestriction . Does it have to do with the fact that the type of xs is polymorphic in the first case but not in the second? I\'d like to know what\'s going on internally. 回答1: The gist

How to define a function in ghci across multiple lines?

假装没事ソ 提交于 2019-11-26 02:40:43
问题 I\'m trying to define any simple function that spans multiple lines in ghci, take the following as an example: let abs n | n >= 0 = n | otherwise = -n So far I\'ve tried pressing Enter after the first line: Prelude> let abs n | n >= 0 = n Prelude> | otherwise = -n <interactive>:1:0: parse error on input `|\' I\'ve also attempted to use the :{ and :} commands but I don\'t get far: Prelude> :{ unknown command \':{\' use :? for help. I\'m using GHC Interactive version 6.6 for Haskell 98 on Linux