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>