下面是上課做練習的一些簡單的代碼
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>