barnameh_riziye_ealani (1)

در نمایش آنلاین پاورپوینت، ممکن است بعضی علائم، اعداد و حتی فونت‌ها به خوبی نمایش داده نشود. این مشکل در فایل اصلی پاورپوینت وجود ندارد.




  • جزئیات
  • امتیاز و نظرات
  • متن پاورپوینت

امتیاز

درحال ارسال
امتیاز کاربر [0 رای]

نقد و بررسی ها

هیچ نظری برای این پاورپوینت نوشته نشده است.

اولین کسی باشید که نظری می نویسد “Pattern Matching & Recursion”

Pattern Matching & Recursion

اسلاید 1: Operators, Functions and Modules1Pattern Matching&Recursion

اسلاید 2: Operators, Functions and Modules2add2 0 b = badd2 a 0 = a add2 a b = a + bA pattern can beA literal 0 ‘a’ True FalseA variable any argument value will match thisWild card “_” any argument value will match thisA tuple pattern (p1, p2, …, pn) matches a tuple with matching valuesA constructorHere is a simple use of patterns in defining a functionadd2 :: Int -> Int -> Int

اسلاید 3: Operators, Functions and Modules3A problemThe following table shows the weekly sales in a company,Week0123456Sales155718705We want to know that given a week n,What is the total sales from week 0 to week nWhat is the maximum weekly sales from week 0 to week nFunction to calculate total sales given a particular week:We will first represent the table of weekly data,sales :: Int -> Intsales 0 = 15sales 1 = 5sales 2 = 7sales 3 = 18sales 4 = 7sales 5 = 0sales 6 = 5

اسلاید 4: Operators, Functions and Modules4totalSales :: Int -> InttotalSales n | n = = 0 = sales 0 | otherwise = totalSales (n-1) + sales nWe 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 clause is called the recursive case.totalSales 4 = 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 = ((((sales 0 + sales 1) + sales 2) + sales 3 ) + sales 4 = 15+5 + 7+18+7+0+5=57Definition of totalSales (using guards):

اسلاید 5: Operators, Functions and Modules5maxSales 4 = maxi (sales 4) maxSales 3 = maxi 7 (maxi (sales 3) maxSales 2) = maxi 7 (maxi 18 (maxi (sales 2) maxSales 1)) = maxi 7 (maxi 18 (maxi 7 (maxi (sales 1) maxSales 0))) = maxi 7 (maxi 18 (maxi 7 (maxi 5 sales 0))) = maxi 7 (maxi 18 (maxi 7 (maxi 5 15))) = maxi 7 (maxi 18 (maxi 7 15)) = maxi 7 (maxi 18 15) = maxi 7 18 = 18Function to calculate maximum sales(using guards):maxSales :: Int -> IntmaxSales n | n = = 0 = sales 0 | otherwise = maxi (sales n) maxSales (n-1)

اسلاید 6: Operators, Functions and Modules6Pattern 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 0totalSales n = totalSales (n-1) + sales nmaxSales 0 = sales 0maxSales 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 -> BoolisZero 0 = TrueisZero _ = FalsePrelude> isZero ‘A’

اسلاید 7: Operators, Functions and Modules7More Types

اسلاید 8: Operators, Functions and Modules8The ASCII code of the characters can also be used for representing them. For instance, ‘65’ is equivalent to ‘A’. CharacterType Char is a Haskell built-in type. Characters are put inside single quotes. ‘a’ to ‘z’, ‘A’ to ‘Z’, ‘0’ to ‘9’Some characters are represented using a backslash “” before them. Examples are:tab ‘t’, newline ‘n’, backslash ‘’single quote ‘’’, double quote ‘”’ASCII codes 65 to 90 represent A-ZASCII codes 97 to 122 represent a-zASCII codes 48 to 57 represent 0-9

اسلاید 9: Operators, Functions and Modules9Prelude> ord ‘r’114Prelude> ‘114’rPrelude> chr (114)rHere is a function for converting a lower case letter to capital.offset :: Intoffset = ord ‘A’ – ord ‘a’toCapital :: Char -> ChartoCapital ch = chr (ord ch + offset)Here, we did not have to define offset. We could have simply said:toCapital ch = chr (ord ch + (ord ‘A’ – ‘ord ‘a’))It is however good practice to define offset as ewe have.There are two useful built in conversion functions.chr :: Int -> Charord :: Char -> Int

اسلاید 10: Operators, Functions and Modules10Other functionstoLowr:: Char -> ChartoLowr ch = chr (ord ch - offset)isDigit :: Char -> Bool isDigit ch = (‘0’ <= ch) && (ch <= ‘9’)isChar :: Char -> BoolisChar ch = not (isDigit ch)

اسلاید 11: Operators, Functions and Modules11The operator ++ used above is the concatenation operator.Prelude>putStr “Massey University”Massey UniversityPrelude> putStr “99u116”cutPrelude>putStr “orangesnapplesnpears”Strings:A string is a special list consisting of characters. Type String = [Char]Here are some strings:“Haskell is a functional programming language.””72el108ot world”“Haskell “ ++ “ programming”orangesapplespears

اسلاید 12: Operators, Functions and Modules12Floating Point NumbersType Float is used for calculations involving floating point numbers.Examples of floating point numbers:110.3421345.365 4.0-2.09Type Float is not used very much. Floating point arithmetic is not very precise in Haskell because of the limited space allowed for theinternal representation of floating point numbers. A type like Double may be used for more precision.

اسلاید 13: Operators, Functions and Modules13Float ->Float -> Float+, *, -, /, ** (example: x ** y )Float ->Float cos, sin, tan, abs, sqrt (square root)Float ->Int -> Float^ (example: x^n)Float ->Intceiling, floor, round, truncate Integer ->FloatfromIntegerFloatpi (the constant pi)An example: sin (pi/2) * piSome built-in floating point operations:

اسلاید 14: Operators, Functions and Modules14Using Integer and Float answer :: Integer answer=42Main> answer + 2.8ERROR - Unresolved overloading*** Type : Fractional Int => Int*** Expression : answer + 2.8Prelude> answer + truncate 2.844Prelude> fromInteger answer + 2.844.8Prelude> floor 2.82Prelude> ceiling 2.83Prelude> round 2.83Prelude> truncate 2.82

اسلاید 15: Operators, Functions and Modules15Function to compute average of weekly salesmeanSales :: Integer -> FloatmeanSales n = fromInteger (totalSales n) / fromInteger (n+1)

20,000 تومان

خرید پاورپوینت توسط کلیه کارت‌های شتاب امکان‌پذیر است و بلافاصله پس از خرید، لینک دانلود پاورپوینت در اختیار شما قرار خواهد گرفت.

در صورت عدم رضایت سفارش برگشت و وجه به حساب شما برگشت داده خواهد شد.

در صورت نیاز با شماره 09353405883 در واتساپ، ایتا و روبیکا تماس بگیرید.

افزودن به سبد خرید