Guards are ways to define functions based on conditions. They allow you to test properties of the function's arguments and choose the appropriate function body based on those tests.
abs n
| n >= 0 = n
| otherwise = -n
How this works?
function is named abs which takes 1 argument n
The symbol | introduces guard.
After each |, there's a boolean expression like n >= 0
If the boolean is true, corresponding expression after the = is used
Guards are evaluated from top to bottom.
The first True guard is chosen.
Typically last guard is used to catch all remaining cases.
classify :: Int -> String
classify n
| n < 0 = "Negative"
| n == 0 = "Zero"
| n < 10 = "Small positive"
| n < 100 = "Medium positive"
| otherwise = "Large positive"
7. Where & Let
These are used for local definitions. These allows us to create intermediate values or helper functions that are only used within a specific function.
f x y = result
where result = x * y + 150
f 5 10 -- returns 150
These where definitions are visible throughout the entire function body, even before they're defined.
the let...in expression in Haskell is a way to define local variables or functions that are only used in specific part of the code.
let <definitions> in <expression>
let x = 5
y = 10
in x + y
wherever there is let, follow ins
The let introduces local bindings, and the in specifies where these bindings are used.
8. Higher Order Functions:
Functions that takes other functions as arguments.
map double [1, 2, 3] -- results in [2, 4, 6]
foldr:
Fundamental higher-order function used list recursion.
Powerful tool for processing list from right to left.
type signature
foldr :: (a -> b -> b) -> b -> [a] -> b
(a -> b ->b), a function that takes an element of the list type a & the accumulated result type b, returns a new result type b.
b: initial value for the accumulator.
[a]: the input list
b: the final result
foldr works by applying the given function to each elements of the list, starting from the right to left.
The accumulator is a value that's carried along as foldr process the list. It stores intermediate result of the computation.
foldr f z [a, b, c]
f a (f b (f c z))
z is the intial accumulator value
f is applied to c(the rightmost element) & z
the result becomes the new accumulator
f is applied to b, & new accumulator
this process continues until all element are processed.
1. Function Definition:
Functions are defined using equations:
2. Function Application:
means apply function f to arguments x & y
3. List Basics
4. The cons operator (:)
adds an element to the front of a list:
5. Pattern Matching:
Uses pattern matching for function definitions:
6. Guards:
Guards are ways to define functions based on conditions. They allow you to test properties of the function's arguments and choose the appropriate function body based on those tests.
How this works?
7. Where & Let
These are used for local definitions. These allows us to create intermediate values or helper functions that are only used within a specific function.
These where definitions are visible throughout the entire function body, even before they're defined.
the let...in expression in Haskell is a way to define local variables or functions that are only used in specific part of the code.
8. Higher Order Functions:
Functions that takes other functions as arguments.
foldr:
foldr works by applying the given function to each elements of the list, starting from the right to left.
The accumulator is a value that's carried along as foldr process the list. It stores intermediate result of the computation.
9. Lambda functions:
Anonymous functions, that are defined without being bound to an identifier. Called lamba functions because of their origins in lambda calculus.
More examples of lambda fucntions:
using lambda in higher order functions
Lambda function that uses pattern matching:
10. Type Declarations:
You can explicitly declare types
11. List Comprehension:
Some more examples of list comprehension:
12. Currying
All function in Haskell are curried by default.
13. keywords
how to use Maybe?
For example of finding 2nd element in the list.
14. List Indexing Operator:
!!, is used for list indexing.
#haskell #basicSyntax #programmingLanguage #functionalProgramming