Learning-simple Haskell code

Source: Internet
Author: User

Below are some simple exercises in classCode
Double X = x + x
Quadruple x = double (Double X)

Increment: int-> int
Increment n = (n + 1)
Increment function. The parameter is an int-type variable and the return value is an int-type variable.

Larger: int-> int
Larger a B = If A> B then a else B
Larger function, two int-type variables. Int-type variables are returned.
The execution effect in hugs is:
> Larger 2 3
False
> Larger 3 2
True

Equal: int-> bool
Equal a B = if a = B then true else false

Lengtha: [int]-> int
Lengtha [] = 0
Lengtha (X: XS) = 1 + lengtha (XS)
The lengtha parameter is an integer array.
The execution effect in hugs is:
> 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)
-- Calculate the number of array elements smaller than 10
The execution effect in hugs is:
> 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-> bool
Greater a B = If (A> B) then true else false

Myfilter2: :( 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 uses the greater function as the first parameter, the second parameter is an integer array, and the third parameter is an integer variable.
Returns an integer array. This function is used to return an array consisting of more than 10 elements.
Execution results in hugs:
Main> myfilter2 greater [1, 2, 3, 4, 5] 2
[3, 4, 5]

The legendary split line
<EOF>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.