صفحه 1:
Pattern Matching & Recursion Operators, Functions and Module

صفحه 2:
Here is a simple use of patterns in defining a function add2 :: Int -> Int -> Int idd2 0 b idd2 a 0 b b a idd2 a at +b \ pattern can be A literal 0 ‘a’ True False Y A variable any argument value will match this Y Wild card “_” any argument value will match this vA tuple pattern (p19°p2e's gin} matches a tuple with ? Modul ‘teen hin acme aon leases:

صفحه 3:
‘oblem following table shows the weekly sales in a company, Week 0 1 2 3 4 5 6 Sales 15) 5 7 18 7 0 5 We want to know that given a week n, What is the total sales from week 0 to week n What is the maximum weekly sales from week 0 to week n Function to calculate total sales given a particular gate’: Int -> Int Weowill-first represent the table of weekly data, sales 1= 5 sales 2 = 7 Operators, Functions and 3 Modules

صفحه 4:
finition of totalSales (using guards): totalSales :: Int -> Int totalSales n |jne= = sales 0 | otherwise = totalSales (n-1) + sales n We have used recursion in the definition of our functions. This type of recursion is called primitive recursion. The first clause is called the base case and the second tbtakSaleschlled the recursive case. = totalSales 3 + sales 4 = (totalSales 2 + sales 3) + sales 4 = ((totalSales 1 + sales 2) + sales 3 )+ sales 4 = (((totalSales 0 + sales 1) + sales 2) + sales 3 )+ sales 4 Operators, Functions and 4 = ((((sales 0 + sales 1) ¥°@¥fes 2) + sales 3 )

صفحه 5:
ction to calculate maximum sales(using quards): maxSales :: Int -> Int maxSales n ۱ ‏د‎ - = sales 0 | otherwise = maxi (sales n) maxSates tt ales 4 ۱22 (sales 4) maxSales 3 _ 1axi 7 (maxi (sales 3) maxSales 2) laxi 7 (maxi 18 (maxi (sales 7 ‘maxSales 1(( ۱221 7 (maxi 18 (maxi 7 maxi (sales 1) maxSales 0))) laxi 7 (maxi 18 (maxi 7 (maxi 5 __ sales 0))) 1axi 7 (maxi 18 (maxi 7 (maxi 5 15((( laxi 7 (maxi 18 (maxi 7 15)) laxi 7 (maxi 18 15) 2 58 Operators, Functions and 5 8 Modules

صفحه 6:
Pattern matching could have been used for defining these functions. Pattern matching may be used when we have a number of equations. Each equation can become a pattern. Let’s now define totalSales and maxSales using patterns. totalSales 0 = sales 0 totalSales n = totalSales (n-1) + sales n maxSales 0 = sales 0 maxSales n = maxi (sales n) (maxSales (n-1)) The underscore “ ” (known as don’t care) can be used as a pattern and we use it when we do not care about the value it matches with. isZero :: Int -> Bool isZero 0 = True isZero Operators, Functions and 6 False Modules

صفحه 7:
More Types Operators, Functions and Modules

صفحه 8:
Character Char is a Haskell built-in type. Characters are put inside si .۹ "70009 ,2 ها 2 رد نم 1 Some characters are represented using a backslash “\” before them. Examples aretab ‘\t’, newline ‘\n’, backslash ‘\V’ single quote ‘\’’, double quote ‘\ The ASCII code of the characters can also be used for representing them. For instarren aes Bye U Ve esent ‏م‎ ‎ASCII codes 97 to 122 represent a-z ASCII codes 48 to 57 represent 0-9 Operators, Functions and Modules

صفحه 9:
There are two useful built in conversion functions. chr :: Int -> Char ide> ordre :: Char -> Int ide> ‘\114’ ide> chr (114) is a function for converting a lower case letter to capital. : Int = ord ‘A’ - ord ‘a’ pital :: Char -> Char pital ch = chr (ord ch + offset) , we did not have to define offset. We could have simply sai pital ch = chr (ord ch + (ord ‘A’ - ‘ord ‘a’)) however good practice to define offset as ewe have. Operators, Functions and 9 Modules

صفحه 10:
Other functions toLowr:: Char -> Char toLowr ch = chr (ord ch - offset) isDigit :: Char -> Bool isDigit ch = (‘0’ <= ch) && (ch <= ‘9’) isChar :: Char -> Bool isChar ch = not (isDigit ch) Operators, Functions and 10 Modules

صفحه 11:
Strings: A string is a special list consisting of characters. Type String = [Char] Here are some strings: “Haskell is a functional programming language.” "\72el\1080\t world” operator Haskell.g FdovO TOMAVHHESenation operator. ide>putStr “Massey University” sey University ide> putStr “\99u\116” ide>putStr “oranges\napples\npears” oranges apples Operators, Functions and 11 pears Modules

صفحه 12:
Floating Point Numbers Type Float is used for calculations involving floating point numbers. Examples of floating.peint numbers: 345.365 4.0 -2.09 Type Float is not used very much. Floating point arithmetic is not very precise in Haskell because of the limited space allowed for the internal representation of floating point numbers. A type like Double Operators, Functions and 12 Modules

صفحه 13:
yme built-in floating point operations: Float ->Float -> Float +551" (example: x** y:) Float ->Float cos, sin, tan, abs, sqrt (square root) Float ->Int -> Float “~ (example: x*n) Float ->Int ceiling, floor, round, truncate Integer ->Float fromInteger Float pi (the constant pi) An example: sin (pi/2) * pi Operators, Functions and Modules

صفحه 14:
Using Integer and Float [answer=t2 Main> answer + 2.8 ERROR - Unresolved overloading ۷ Type : Fractional Int => Int * Expression : answer + 2.8 Prelude> answer + truncate 2.8 44 Prelude> fromInteger answer + 2.8 44.8 Prelude> floor 2.8 2 Prelude> ceiling 2.8 3 Prelude> round 2.8 Prelude> truncate 2.8 Operators, Functions and 14 2 Modules

صفحه 15:
15 Function to compute average of weekly sales Operators, Functions and Modules

جهت مطالعه ادامه متن، فایل را دریافت نمایید.
34,000 تومان