Any way to add patterns, type signature, to a function in GHCi?

℡╲_俬逩灬. 提交于 2019-12-24 17:19:01

问题


^-- No, it doesn't entirely. My question covers ADDING patterns and type signatures interactively...which is apparently impossible.

The most basic things you might try do from early tutorials won't work in GHCi:

foo [] = []
foo (x:xs) = x : foo xs

That works if you put it into foo.hs and at the GHCi prompt type :load foo.hs. You can then invoke foo on a list and get the list back.

Early Google searches tell you that in GHCi you need a let statement. But in this case (a function defined with multiple patterns) it won't work:

Prelude> let foo [] = []
Prelude> let foo (x:xs) = x : foo xs
Prelude> foo [1, 2, 3]
[1,2,3*** Exception: <interactive>:3:5-27: Non-exhaustive patterns 
    in function foo

The second "let" overwrote the first "let". Leaving out the let isn't an option. And it doesn't like it if you type in expressions like foo :: [a] -> [a] either.

The tutorials seem to sidestep this and send you quickly into putting your code into files. What if you don't want to make a file, and want to work interactively? What are the options?


回答1:


No. You cannot add new patterns or a type signature to a function in GHCi after you have run a statement defining it in the evaluator.

For that matter, you shouldn't see it as "adding" on a line-by-line basis in source code, either. It's merely a notation convenience. So when you look at a "multi-line" definition like:

foo :: [a] -> [a]
foo [] = []
foo (x:xs) = x : foo xs

Those foo definitions must all be tied together as a group. You can't split them up...so for instance, this will cause an error:

foo :: [a] -> [a]
foo [] = []

bar = 3

foo (x:xs) = x : foo xs

(Note: The type signature may be separated, however. For the details of how far it can be separated, see How is 'block' granularity in Haskell defined? )

To put things together as a group, you can use multi-line input in GHCi. Or you could do it all on one line with semicolons:

let foo :: [a] -> [a] ; foo [] = [] ; foo (x:xs) = x : xs

But you can't type in an input, test it, then patch individual patterns in and out, and test it again. The whole function is redefined with each let.




回答2:


Use multiline input:

Prelude> :set +m
Prelude> let
Prelude| foo [] = []
Prelude| foo (x:xs) = x : foo xs
Prelude| 
Prelude> foo [1,2,3]
[1,2,3]


来源:https://stackoverflow.com/questions/24977706/any-way-to-add-patterns-type-signature-to-a-function-in-ghci

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!