免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9706 | 回复: 10
打印 上一主题 下一主题

haskell中函数只有一个参数吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-29 16:37 |显示全部楼层 |倒序浏览
(+) a b
=> ((+)a) b

其实该计算过程涉及到两个函数吧?

那我们该说 (+) 函数有两个参数还是有一个参数?

[ 本帖最后由 izhier 于 2009-3-29 17:16 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-03-29 17:05 |显示全部楼层
改了一下标题,锁定 haskell 语言,不是太了解其他函数式语言。
原帖由 win_hate 于 2009-3-29 16:56 发表
如果给它三个参数,会出错的。

(+) 的类型是:Num a => a -> (a -> a),出错是因为类型不匹配,不是参数数目不匹配吧 ?
这不能判断 (+) 有两个参数吧?

[ 本帖最后由 izhier 于 2009-3-29 17:20 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-03-29 17:51 |显示全部楼层
原帖由 win_hate 于 2009-3-29 17:23 发表
>> 改了一下标题,锁定 haskell 语言,不是太了解其他函数式语言。

我没说其它语言啊

恩,上面是我理解有误

yaht 中 4.4.2 节 Higher-Order Types 有说:

  1. (+) :: Num a => a -> a -> a
  2. + is a function which, for some type a which is an instance of Num, takes a value of
  3. type a and produces another function which takes a value of type a and produces a value
  4. of type a. In short hand, we might say that + takes two values of type a and produces a
  5. value of type a, but this is less precise.
复制代码

这种说法是只用于
说明函数的类型
还是
也说明函数的执行过程呀?

[ 本帖最后由 izhier 于 2009-3-29 18:03 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2009-03-29 19:00 |显示全部楼层
原帖由 Magicloud 于 2009-3-29 18:25 发表
haskell中的體現就是
inc = (+) 1
使用參數省略定義的函數……

这个不是参数省略的函数吧
它应该本身就是一个函数

  1. Hugs.Base> :t (+) 1
  2. (1 +) :: Num a => a -> a
复制代码

论坛徽章:
0
5 [报告]
发表于 2009-03-29 19:26 |显示全部楼层
flw 这么一说
我更加没头脑了
这个题目不知是用数学分析还是 lambda 演算
函数式编程应该比较倾向于 lambda 演算吧?

[ 本帖最后由 izhier 于 2009-3-29 19:36 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2009-03-30 12:51 |显示全部楼层
再一个问题:

haskell 中有没有 无参数 的函数呀?

论坛徽章:
0
7 [报告]
发表于 2009-03-30 16:50 |显示全部楼层
原帖由 win_hate 于 2009-3-30 13:18 发表
常数是无参函数

啊...常数都是函数呀!

那是不是 haskell 里 "一切皆函数" ?
就像 python 里 "一切皆对象"

论坛徽章:
0
8 [报告]
发表于 2009-04-01 20:18 |显示全部楼层

  1. --test.hs
  2. module Main where

  3. data Student = Student {
  4.                          stId   :: Int,
  5.                          stName :: String
  6.                 } deriving (Show)
复制代码

  1. :l test.hs
  2. Main> Student 123 "abc"
  3. Student {stId = 123, stName = "abc"}
  4. Main> stId(Student 123 "abc")
  5. 123
  6. Main> stName(Student 123 "abc")
  7. "abc"
复制代码

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

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

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

论坛徽章:
0
9 [报告]
发表于 2009-04-13 15:02 |显示全部楼层
原帖由 win_hate 于 2009-4-1 21:15 发表
看 Student 的类型:
应该是一个函数。

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

  1. module Main where

  2. data Shape = Circle Float
  3.            deriving ( Show )

  4. area :: Shape -> Float
  5. area (Circle r) = pi * r ^ 2
复制代码

解释器运行如下:

  1. *Main> :t area
  2. area :: Shape -> Float
  3. *Main> :t Circle
  4. Circle :: Float -> Shape
  5. *Main> area $ Circle 2
  6. 12.566371
  7. *Main> (area.Circle) 2               --这个功能也可行
  8. 12.566371
  9. *Main> area.Circle $ 2
  10. 12.566371
复制代码

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

论坛徽章:
0
10 [报告]
发表于 2009-04-13 15:10 |显示全部楼层

回复 #27 flw 的帖子


. 函数的优先级挺高的

  1. *Main> :i (^)
  2. (^) :: (Num a, Integral b) => a -> b -> a       -- Defined in GHC.Real
  3. infixr 8 ^
  4. *Main> :i (.)
  5. (.) :: (b -> c) -> (a -> b) -> a -> c   -- Defined in GHC.Base
  6. infixr 9 .
复制代码

[ 本帖最后由 izhier 于 2009-4-13 15:13 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP