()函数变量和应用程序中 [英] () in Function Variable and Application

查看:105
本文介绍了()函数变量和应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型和功能讲座提供了以下功能:

  f44 ::() - >整数
f44()= 44

我输入以下内容:

  ghci>让f()= 5 
ghci> f()
5

但是,我很困惑() let f()中。通常,作为初学者,我在函数名后面看到了一个不可变变量,即 f



let f ... 之后列出的()的名称。如何在函数应用程序中使用 f()

解决方案

<在Haskell中,它既是一个类型的名称,如

中所见<$ p $>
$ b

  f44 ::() - >整数

以及该类型的唯一值的名称,如

  f44()= 44 

它用于模式匹配。



这个比较熟悉的定义将提供一个等效但更详细的类型:

  data单位=单位
f45 ::单位 - >整数
f45单位= 45

没有什么能阻止你绑定( )转换为名称,就像您可以获得任何其他值一样:

  Prelude>让f()= 5 
Prelude> :t f
f :: Num a => () - > a
Prelude>让name =()
Prelude> :t名称
名称::()
前奏>名称
()
前奏> f名称
5


The Types and Functions lecture presents the function:

f44 :: () -> Integer
f44 () = 44

I typed the following:

ghci> let f () = 5
ghci> f ()
5

But, I'm confused by the () in let f (). Typically, as a beginner, I've seen an immutable variable following the function name, i.e. f.

What is the name of () when it's listed after let f ...? How about when it's used in the function application, f ()?

解决方案

"()" is usually pronounced "unit".

In Haskell, it is both the name of a type, as seen in

f44 :: () -> Integer

and the name of the only value that exists of that type, as seen in

f44 () = 44

where it is used for pattern matching.

This more familiar-looking definition would provide an equivalent but more verbose type:

data Unit = Unit
f45 :: Unit -> Integer
f45 Unit = 45

Nothing stops you from binding () to a name, just like you can for any other value:

Prelude> let f () = 5
Prelude> :t f
f :: Num a => () -> a
Prelude> let name = ()
Prelude> :t name
name :: ()
Prelude> name
()
Prelude> f name
5

这篇关于()函数变量和应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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