You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Haskell is a statically typed, purely functional programming language known for its expressive syntax and powerful type system. Haskell's syntax emphasizes clarity and mathematical precision. Functions and immutability are at the heart of Haskell.
Similar to where, but can be used wherever expressions are allowed.
cylinder::Double->Double->Double
cylinder r h =let sideArea =2*pi* r * h
topArea =pi* r ^2in sideArea +2* topArea
13. Case Expressions
Provides pattern matching in expressions.
describeList:: [a] ->String
describeList xs =case xs of[]->"The list is empty."
[x] ->"The list has one element."
(x:xs) ->"The list has multiple elements."
14. Higher-Order Functions
Functions that take other functions as arguments or return them.
applyTwice:: (a->a) ->a->a
applyTwice f x = f (f x)
Using Maybe and Either for computations that may fail.
findElement::Eqa=>a-> [a] ->MaybeInt
findElement _ []=Nothing
findElement y (x:xs)
| y == x =Just0|otherwise=fmap (+1) (findElement y xs)
25. Input and Output
Reading and Writing Files:
main::IO()
main =do
contents <-readFile"input.txt"writeFile"output.txt" (process contents)
process::String->String
process =map toUpper
26. Common Prelude Functions
map: Applies a function to every element of a list.
squares =map (^2) [1..5] -- [1,4,9,16,25]
filter: Selects elements of a list that satisfy a predicate.
evens =filtereven [1..10] -- [2,4,6,8,10]
foldl and foldr: Reduce a list to a single value.
sumOfList =foldl(+)0 [1..5] -- 15
27. Handling Exceptions
Using Control.Exception:
importControl.Exceptionmain::IO()
main =do
result <- try (readFile"nonexistent.txt") ::IO (EitherIOErrorString)
case result ofLeft ex ->putStrLn$"An error occurred: "++show ex
Right contents ->putStrLn contents
References:
1. Basic Structure of a Haskell Program
Every Haskell program consists of functions and definitions organized within modules.
module
keyword.main
function is the entry point of a Haskell program.main :: IO ()
specifies thatmain
is an IO action returning nothing (()
).2. Comments
Single-line Comments: Begin with
--
.-- This is a single-line comment
Multi-line Comments: Enclosed between
{-
and-}
.3. Variables and Functions
Variable Definitions: Assigned using
=
.Function Definitions: Also use
=
, with parameters specified after the function name.Type Signatures: Optional but recommended for clarity.
Num a =>
means for any typea
that is an instance of theNum
class.4. Function Application
Syntax: Functions are applied by placing arguments after the function name, separated by spaces.
Parentheses: Used to group expressions.
5. Operators
+
,-
,*
,/
,^
(exponentiation),div
,mod
.==
,/=
,<
,>
,<=
,>=
.&&
(and),||
(or),not
.6. Conditional Expressions
if
expressions rather than statements.7. Lists
Creating Lists:
List Operations:
++
:
, adds an element to the front.Ranges:
List Comprehensions:
8. Tuples
Fixed-size collections that can hold different types.
Accessing tuple elements using pattern matching.
getName (name, _) = name
9. Pattern Matching
Define functions by specifying patterns.
Pattern matching can be used in function parameters,
case
expressions, andlet
bindings.10. Guards
11. Where Clauses
12. Let Expressions
where
, but can be used wherever expressions are allowed.13. Case Expressions
14. Higher-Order Functions
15. Lambda Functions
\
syntax.16. Type Classes and Instances
Type Classes: Define a set of functions that can be implemented by types.
Instances: Implement a type class for a specific type.
17. Data Types
Define custom data types using
data
.Example with Record Syntax:
18. Recursion
19. Input/Output (IO)
20. Monads
Abstract data types used to represent computations.
Maybe Monad:
Using
do
Notation with Monads:21. List Comprehensions
22. Modules and Imports
Defining a Module:
Importing Modules:
23. Lazy Evaluation
Expressions are evaluated only when needed.
Infinite Lists:
Example with
take
:24. Error Handling
Maybe
andEither
for computations that may fail.25. Input and Output
26. Common Prelude Functions
map
: Applies a function to every element of a list.filter
: Selects elements of a list that satisfy a predicate.foldl
andfoldr
: Reduce a list to a single value.27. Handling Exceptions
Using
Control.Exception
:28. Using Packages with Cabal or Stack
The text was updated successfully, but these errors were encountered: