使用 GHCi 时如何为函数提供显式类型声明?

发布于 2024-09-07 01:38:45 字数 728 浏览 1 评论 0 原文

如何在 GHCi 中定义此函数的等效项(取自 learnyouahaskell)?

import Data.List  

numUniques :: (Eq a) => [a] -> Int  
numUniques = length . nub  

如果没有类型声明,GHCi 会接受函数定义,但最终会得到一个无用的类型:

Prelude Data.List> import Data.List 
Prelude Data.List> let numUniques' = length . nub
Prelude Data.List> :t numUniques'
numUniques' :: [()] -> Int

生成的函数仅接受单位列表作为参数。

有没有办法在 GHCi 中提供类型声明?或者是否有另一种方法来定义不需要类型声明的函数?

我在 GHCi 指南中没有看到明显的线索,并尝试了如下的表达式(无济于事):

> let numUniques' = ((length . nub) :: (Eq a) => [a] -> Int)
> :t numUniques'
numUniques' :: [()] -> Int

How to I define the equivalent of this function (taken from learnyouahaskell) inside GHCi?

import Data.List  

numUniques :: (Eq a) => [a] -> Int  
numUniques = length . nub  

Without the type declaration, GHCi accepts the function definition, but it ends up with an unhelpful type:

Prelude Data.List> import Data.List 
Prelude Data.List> let numUniques' = length . nub
Prelude Data.List> :t numUniques'
numUniques' :: [()] -> Int

The resulting function only accepts a list of units as a parameter.

Is there a way provide type declarations in GHCi? Or is there another way to define functions like these which doesn't require type declarations?

I saw no obvious clues in the GHCi guide, and experimented with expressions like the following (to no avail):

> let numUniques' = ((length . nub) :: (Eq a) => [a] -> Int)
> :t numUniques'
numUniques' :: [()] -> Int

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆头 2024-09-14 01:38:45

有没有办法在 GHCi 中提供类型声明?

let numUniques' :: (Eq a) => [a] -> Int; numUniques' = length . nub

或者是否有另一种方法来定义不需要类型声明的函数?

如果您使用 -XNoMonomorphismRestriction 关闭单态限制,它将推断出正确的类型。

Is there a way provide type declarations in GHCi?

let numUniques' :: (Eq a) => [a] -> Int; numUniques' = length . nub

Or is there another way to define functions like these which doesn't require type declarations?

If you turn off the monomorphism restriction with -XNoMonomorphismRestriction, it will infer the right type.

刘备忘录 2024-09-14 01:38:45

GHC 用户指南显示了实现此目的的另外两种方法。 本小节 引入了 :{ ... :} 构造,可以按如下方式使用:

> :{
| numUniques :: (Eq a) => [a] -> Int
| numUniques = length . nub
| :}

或者,您可以启用 多行模式

> :set +m
> let
| numUniques :: (Eq a) => [a] -> Int
| numUniques = length . nub
| 

The GHC User's Guide shows two additional ways to achieve this. This subsection introduces the :{ ... :} construct, which can be used as follows:

> :{
| numUniques :: (Eq a) => [a] -> Int
| numUniques = length . nub
| :}

Alternatively, you can enable multiline mode:

> :set +m
> let
| numUniques :: (Eq a) => [a] -> Int
| numUniques = length . nub
| 
清风不识月 2024-09-14 01:38:45

请注意,您还可以通过将“点”(即显式变量)添加回表达式来避免单态性限制。所以这也给出了正确的类型:

让 numUniques x = 长度 。结点$x

Note that you can also avoid the monomorphism restriction simply by adding "points" (i.e. explicit variables) back to your expression. So this also gives the correct type:

let numUniques x = length . nub $ x

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文