>>=
operation is defined. Haskell’s I/O is based on Monads. factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n - 1)​
dayToNumber :: String -> Int
dayToNumber day = case day of
"Monday" -> 1
"Tuesday" -> 2
"Wednesday" -> 3
"Thursday" -> 4
"Friday" -> 5
"Saturday" -> 6
"Sunday" -> 7
_ -> error "Invalid day"​
In this example, the case expression matches the value of `day` against different patterns (day names) and returns the corresponding number. The underscore `_` serves as a catch-all pattern that matches any value and indicates an error for invalid inputs.sumList :: [Int] -> Int
sumList [] = 0
sumList (x:xs) = x + sumList xs​
`$`
operator has the following type signature: `($) :: (a -> b) -> a -> b
`.f $ x
` is equivalent to `f x
`.`$`
operator is to eliminate the need for explicit parentheses when applying a function to an argument or composing functions. -- Without using $
result1 = sin (cos (sqrt 2))
-- Using $
result2 = sin $ cos $ sqrt 2​
result1
` and `result2
` are equivalent. The `$`
operator allows for a more concise and readable expression by avoiding nested parentheses.
2. The Dot Operator (Composition) :(.) :: (b -> c) -> (a -> b) -> a -> c
`.(f . g) x
` is equivalent to `f (g x)
`. add1 :: Int -> Int
add1 x = x + 1
double :: Int -> Int
double x = x * 2
-- Without using .
result3 = double (add1 5)
-- Using .
result4 = double . add1 $ 5​
result3
` and `result4
` are equivalent. The dot operator allows for a more elegant expression by composing the `add1
` and `double` functions. `(.)`
(dot), enabling a concise and declarative style of programming.map
`, `filter
`, and `fold
` are higher-order functions that abstract common control flow patterns, making it easier to express transformations and computations over collections of values.map
`, `filter
`, `foldl
`, `foldr
`, and `zipWith
`. These functions take other functions as arguments to perform operations on lists or other data structures. Higher-order functions provide a way to abstract and generalize computations, leading to more modular and reusable code. getLine
`, `putStrLn
`, or `readFile
`, are specifically designed to handle I/O operations and return IO actions that can be executed in a controlled manner. main :: IO ()
main = do
putStrLn "Enter your name:"
name <- getLine
putStrLn ("Hello, " ++ name ++ "!")​
`id`
function, which has the type `a -> a`
, meaning it works for any type `a`
, and the list type `[]`
, which can hold elements of any type.
2. Ad-hoc Polymorphism (Overloading) :`Eq`
type class, which provides equality comparison (`==`)
and inequality comparison (`/=`)
operations, and the `Ord`
type class, which provides ordering operations (`<`, `>`, `<=`, `>=`)
. data Bool = True | False​
Maybe
` type, which represents an optional value: data Maybe a = Nothing | Just a​
Maybe
` has two alternatives: `Nothing
` represents the absence of a value, and `Just a
` represents a value of type `a`
. data Point = Point { x :: Double, y :: Double }​
Point
` is a product type with two fields: `x`
and `y`
. Each field represents a coordinate value.
ADTs are used in Haskell for a variety of purposes, including :foldl
` and `foldr
` are higher-order functions that allow you to reduce a list to a single value by applying a binary operation to the elements of the list. The key difference between `foldl` and `foldr` lies in their evaluation order and associativity.foldl
` is: `foldl :: (b -> a -> b) -> b -> [a] -> b
`.foldr
` is: `foldr :: (a -> b -> b) -> b -> [a] -> b
`.
Key Differences :foldl
` and `foldr
` depends on the specific use case, desired evaluation order, associativity requirements, and potential performance considerations. Control.Concurrent
` module provides functions for creating and managing lightweight threads in Haskell.Control.Concurrent.STM
` module provides functions and types for working with STM in Haskell.
3. Parallelism using `par` and `pseq` :par
` and `pseq
` combinators.Control.Parallel.Strategies
` module, which offers higher-level constructs for expressing parallelism and controlling evaluation strategies.async
`, `conduit
`, and `pipes
`, which provide additional abstractions and utilities for managing concurrent and parallel computations. Functor
` type class in Haskell defines the `fmap
` function, which allows applying a function to the values inside the functor.fmap
` function has the type signature `fmap :: (a -> b) -> f a -> f b
`, where `f`
is the functor type constructor.Applicative
` type class in Haskell extends the `Functor
` type class and introduces the `pure
` function to lift a value into an applicative functor.`(<*>)`
function, called "apply," which allows applying a function inside an applicative functor to a value inside another applicative functor.(<*>)
` function has the type signature `(<*>) :: f (a -> b) -> f a -> f b
`.Monad
` type class in Haskell defines the `return
` function (equivalent to `pure
`) and the `(>>=)
` function (pronounced "bind
"), which allows sequencing computations within a monadic context.(>>=)
` has the type signature `(>>=) :: m a -> (a
-> m b) -> m b
`, where `m` is the monad type constructor.do
` notation, which provides a more readable syntax for composing monadic computations.