学习-Haskell的简单代码

下面是上课做练习的一些简单的代码
double x = x+x
quadruple x = double( double x)

increment::Int->Int
increment n=(n+1)
increment函数,参数为1个Int型变量,返回值Int型

larger::Int->Int->Int
larger a b = if a>b then a else b
larger函数,参数2个Int型变量,返回Int型
在Hugs里面的执行效果为:
>larger 2 3
False
>larger 3 2
True

equal::Int->Int->Bool
equal a b = if a==b then True else False

lengtha::[Int]->Int
lengtha  [] = 0
lengtha  (x:xs) = 1+lengtha (xs)
lengtha参数是一个整形数组
在Hugs里的执行效果为:
>lengtha [1,2,3]
3


producta::[Int]->Int
producta []=1
producta (x:xs) = x* producta(xs)

count_small::[Int]->Int
count_small [] =0
count_small(x:xs) = if x<10 then 1+count_small(xs) else 0+count_small(xs)
--计算数组元素小于10的个数
在Hugs里的执行效果为:
>count_small [1,2,3,90,31,45]
3

maxa::[Int]->Int
maxa [] =0
maxa (x:xs) = if x<maxa(xs) then maxa(xs) else x


gr_than_tar::Int->[Int]->Bool
gr_than_tar f [] = True
gr_than_tar f (x:xs) = if x<=f then False else  gr_than_tar f (xs)

greater::Int->Int->Bool
greater a b = if(a>b) then True else False

myfilter2::(Int->Int->Bool)->[Int]->Int->[Int]
myfilter2 greater [] t = []
myfilter2 greater (x:xs) t =
              if(greater x t==True) then x: myfilter2 greater xs t
                                   else myfilter2 greater xs t

myfilter2取greater函数作为第一个参数,第二个参数是整形数组,第三个参数是整形变量
返回整型数组,该函数的功能是返回参数数组中大于10 的数组元素组成的数组
在Hugs里的执行效果:
Main> myfilter2 greater [1,2,3,4,5] 2
[3,4,5]   

14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif传说中的分割线14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif14.gif
<EOF>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值