izhier 发表于 2009-04-01 20:18


--test.hs
module Main where

data Student = Student {
                         stId   :: Int,
                         stName :: String
                } deriving (Show)


:l test.hs
Main> Student 123 "abc"
Student {stId = 123, stName = "abc"}
Main> stId(Student 123 "abc")
123
Main> stName(Student 123 "abc")
"abc"

上面 Student (value constructor) 是函数吗?

那 Bool 的两个 value constructor True 和 False 是函数(无参函数)吗?
这两个相当于常量吧?

[ 本帖最后由 izhier 于 2009-4-1 20:22 编辑 ]

win_hate 发表于 2009-04-01 21:15

看 Student 的类型:


*Main> :t Student
Student :: Int -> String -> Student
*Main>


应该是一个函数。

True 和 False 是常量。

flw 发表于 2009-04-02 08:51

对。是函数。这个 YAHT 里讲的很清楚。

win_hate 发表于 2009-04-02 14:21

我看过的 haskell 文档都没有把这个东西直接称为函数,而是叫做建构子。可能因为建构子除了具有一般的函数功能外,还可以用作模式匹配吧。

flw 发表于 2009-04-02 15:15

原帖由 win_hate 于 2009-4-2 14:21 发表 http://bbs3.chinaunix.net/images/common/back.gif
我看过的 haskell 文档都没有把这个东西直接称为函数,而是叫做建构子。可能因为建构子除了具有一般的函数功能外,还可以用作模式匹配吧。
嗯。

izhier 发表于 2009-04-13 15:02

原帖由 win_hate 于 2009-4-1 21:15 发表 http://bbs2.chinaunix.net/images/common/back.gif
看 Student 的类型:
应该是一个函数。

应该是一个函数
看这个例子:

module Main where

data Shape = Circle Float
         deriving ( Show )

area :: Shape -> Float
area (Circle r) = pi * r ^ 2

解释器运行如下:

*Main> :t area
area :: Shape -> Float
*Main> :t Circle
Circle :: Float -> Shape
*Main> area $ Circle 2
12.566371
*Main> (area.Circle) 2               --这个功能也可行
12.566371
*Main> area.Circle $ 2
12.566371


[ 本帖最后由 izhier 于 2009-4-13 15:06 编辑 ]

flw 发表于 2009-04-13 15:05

原帖由 izhier 于 2009-4-13 15:02 发表 http://bbs3.chinaunix.net/images/common/back.gif

应该是一个函数
看这个例子:

module Main where

data Shape = Circle Float
         deriving ( Show )

area :: Shape -> Float
area (Circle r) = pi * r ^ 2

解释器运行如下:

*Main ...
这个小数点可和 C/C++ 的小数点不一样啊。
这个小数点是个运算符。
复合函数。

izhier 发表于 2009-04-13 15:10

回复 #27 flw 的帖子


. 函数的优先级挺高的

*Main> :i (^)
(^) :: (Num a, Integral b) => a -> b -> a       -- Defined in GHC.Real
infixr 8 ^
*Main> :i (.)
(.) :: (b -> c) -> (a -> b) -> a -> c   -- Defined in GHC.Base
infixr 9 .


[ 本帖最后由 izhier 于 2009-4-13 15:13 编辑 ]

izhier 发表于 2009-04-13 15:26

programming in haskell

The difference between normal functions and constructor functions is that the
latter have no defining equations, and exist solely for the purpose of building
pieces of data.
Not surprisingly, data declarations themselves can also be parameterised.

问一下
以上红色的部分怎么翻译呢 ?
都碰到好几回了

难道是可被作为函数参数或可作为模式匹配 ?

[ 本帖最后由 izhier 于 2009-4-13 15:39 编辑 ]

MMMIX 发表于 2009-04-13 17:02

原帖由 izhier 于 2009-4-13 15:26 发表 http://bbs2.chinaunix.net/images/common/back.gif
Not surprisingly, data declarations themselves can also be parameterised.

问一下
以上红色的部分怎么翻译呢 ?

那意思就是说在声明数据类型的时候也可以带参数,看看它的例子应该有帮助。
页: 1 2 [3]
查看完整版本: haskell中函数只有一个参数吗?