任何方式添加模式,键入签名,GHCi函数? [英] Any way to add patterns, type signature, to a function in GHCi?

查看:139
本文介绍了任何方式添加模式,键入签名,GHCi函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

^ - 不,它并不完全。我的问题涉及交互式地添加模式和类型签名......这显然是不可能的。 您可能会尝试在早期教程中做的最基本的事情不会在GHCi工作:

pre $ f $ f $ [ b $ b

如果您将它放入 foo.hs 并在GHCi提示符下键入:load foo.hs 。您可以在列表中调用foo并返回列表。



早期的Google搜索告诉您,在GHCi中,您需要一个 let 语句。但在这种情况下(一个用多种模式定义的函数)它不起作用:

  Prelude> let foo [] = [] 
Prelude>让foo(x:xs)= x:foo xs
Prelude> foo [1,2,3]
[1,2,3 ***例外:< interactive>:3:5-27:函数foo中的非穷举模式

第二个let覆盖了第一个let。离开让是不是一种选择。它不喜欢它,如果你输入像 foo :: [a] - > [a] 要么。



这些教程似乎回避了这一点,并迅速将您的代码放入文件中。如果你不想创建一个文件,并想交互式工作,会怎么样?有什么选择?

解决方案

没有。在运行在评估程序中定义它的语句后,您不能在GHCi中为函数添加新模式或类型签名。



对于这种情况,您不应该看到它在源代码中是逐行添加的。这只是一个符号方便。所以,当你看到像以下这样的多行定义:

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

这些foo定义必须作为一个组合在一起。你不能把它们分开...所以,例如,这会导致一个错误:

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

bar = 3

foo(x:xs)= x:foo xs

a href =https://stackoverflow.com/questions/24978563/how-is-block-granularity-in-haskell-defined> Haskell中的'block'粒度如何定义?)

为了把所有东西放在一起,你可以在GHCi中使用多行输入。或者你可以用分号分隔一行:

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

但是您无法输入输入,测试它然后修补个别模式,并再次测试。整个函数用 let 重新定义。


^-- 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?

解决方案

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.

这篇关于任何方式添加模式,键入签名,GHCi函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆