Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
- Lab Suspended due to Delegation Visit.
  • Loading branch information
ramagururadhakrishnan authored Mar 5, 2024
1 parent 11fc10c commit f2967a7
Showing 1 changed file with 1 addition and 103 deletions.
104 changes: 1 addition & 103 deletions Assets/Lectures/Lab5.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) <br/>
![](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 = "<script>alert('Hello, 21CYS!')</script>"
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 = "<script>alert('Hello, 21CYS!')</script>"
if validateInput userInput
then processUserInput userInput
else putStrLn "Input did not pass validation"
```

### Types
- type
- newType
Expand Down

0 comments on commit f2967a7

Please sign in to comment.