學習-Haskell的簡單代碼

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.