下面是上课做练习的一些简单的代码
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]
传说中的分割线
<EOF>