diff --git a/Assets/Lectures/Lab5.md b/Assets/Lectures/Lab5.md index 590f9a3..abb706f 100644 --- a/Assets/Lectures/Lab5.md +++ b/Assets/Lectures/Lab5.md @@ -2,111 +2,9 @@ ![](https://img.shields.io/badge/Batch-21CYS-lightgreen) ![](https://img.shields.io/badge/UG-blue) ![](https://img.shields.io/badge/Subject-PPL-blue)
![](https://img.shields.io/badge/Lecture-2-orange) ![](https://img.shields.io/badge/Practical-3-orange) ![](https://img.shields.io/badge/Credits-3-orange) -## Lab 5 - Higher Order Functions (Continued), Function Composition and Types +## Lab 5 - Types ![](https://img.shields.io/badge/-05th_Mar-orange) -### foldr and foldl -The `foldr` function (right fold) takes a binary function, an initial accumulator value, and a list. It recursively combines the elements of the list from right to left using the binary function and the accumulator. - -##### Simple Example -``` --- Let's define a function to sum all elements of a list -sumList :: [Int] -> Int -sumList xs = foldr (+) 0 xs - --- Summing all elements of a list -totalSum = sumList [1, 2, 3, 4, 5] --- Result: 15 -``` - -##### Another Example -``` --- Function to calculate the factorial of a number -factorial :: Int -> Int -factorial n = foldr (*) 1 [1..n] - --- Calculating the factorial of 5 -factorialOfFive = factorial 5 --- Result: 120 (1 * 2 * 3 * 4 * 5) -``` - -##### foldl and foldr -``` --- Define a function to subtract two numbers -subtractNum :: Int -> Int -> Int -subtractNum x y = x - y - --- List of numbers -numbersList = [1, 2, 3, 4] - --- Using foldl to perform left fold with subtraction -foldlResult = foldl subtractNum 0 numbersList --- Result: -10 (0 - 1 - 2 - 3 - 4) - --- Using foldr to perform right fold with subtraction -foldrResult = foldr subtractNum 0 numbersList --- Result: 2 (1 - (2 - (3 - (4 - 0)))) -``` - -### Function Composition - -``` - sanitizeInput :: String -> String - sanitizeInput inputString = concatMap replaceAngleBrackets inputString - where - replaceAngleBrackets '<' = "<" - replaceAngleBrackets '>' = ">" - replaceAngleBrackets c = [c] - - validateInput :: String -> Bool - validateInput inputString = length inputString > 5 - - saveToDatabase :: String -> IO () - saveToDatabase inputString = putStrLn $ "Saving to database: " ++ inputString - - -- Function composition - processUserInput :: String -> IO () - processUserInput x = saveToDatabase (sanitizeInput x) - - -- Example usage: - main :: IO () - main = do - let userInput = "" - if validateInput userInput - then processUserInput userInput - else putStrLn "Input did not pass validation" -``` - -Another form - -``` -sanitizeInput :: String -> String -sanitizeInput inputString = concatMap replaceAngleBrackets inputString - where - replaceAngleBrackets '<' = "<" - replaceAngleBrackets '>' = ">" - replaceAngleBrackets c = [c] - -validateInput :: String -> Bool -validateInput inputString = length inputString > 5 - -saveToDatabase :: String -> IO () -saveToDatabase inputString = putStrLn $ "Saving to database: " ++ inputString - --- Function composition -processUserInput :: String -> IO () -processUserInput = saveToDatabase . sanitizeInput - --- Example usage: -main :: IO () -main = do - let userInput = "" - if validateInput userInput - then processUserInput userInput - else putStrLn "Input did not pass validation" - -``` - ### Types - type - newType