| Safe Haskell | Safe-Inferred |
|---|
Lessons.Lesson03
Description
Notes taken by Ugnė Pacevičiūtė
Synopsis
- sumOfInts :: [Int] -> Int
- sumOfInts' :: [Int] -> Int
- data Dumpable = Examples
- data Command
- class FuzzyAdd a where
- (~+~) :: a -> a -> a
- a1 :: Integer
- a2 :: Integer
- data FireExtinguisher = FireExtinguisher {}
- fe :: FireExtinguisher
- fe2 :: FireExtinguisher
- cpcty :: FireExtinguisher -> Integer
- data Bucket = Bucket {
- bucketCapacity :: Integer
- safeHead :: [a] -> Maybe a
- safeHeadDefault :: [a] -> a -> a
Documentation
sumOfInts :: [Int] -> Int Source #
We do not have loops in Haskell, because loops require a mutable state (e.g. a counter), so we use recursion! This function sums all integers in a given list. If the list is empty, it returns 0 as the base case. Otherwise, it takes the first element (the head) and adds it to the result of summing the rest of the list (the tail).
sumOfInts' :: [Int] -> Int Source #
This function also recursively sums all integers in a given list. But it has an advantage: instead of accumulating of intemediate results (needed for + function) in stack, you can accumulate these values in a dedicated argument, usually called "accumulator". This approach is called "tail recursion" and modern compiles are smart enough to identify this technic and optimize it (rewrite it with a machine code loop) so no stack at all is used.
In Haskell, a typeclass works similarly to an interface in Java.
The Command type is an example of an Algebraic Data Type (ADT).
It represents a set of possible "commands" that a program might handle.
The left-hand side (Command) defines the type name.
The right-hand side lists the constructors (Dump and Sum).
Each constructor defines a different *form* that a Command value can take:
Dump wraps a Dumpable value.
Sum wraps a list of integers that can be processed.
Constructors
| Examples |
Instances
| Show Dumpable Source # | This instance tells Haskell how to display values of type |
Instances
| Show Command Source # | Here we define a custom |
class FuzzyAdd a where Source #
The FuzzyAdd typeclass represents a group of types that can be
combined using an “fuzzy” addition operation.
The operator (~+~) defines the behavior for this custom kind of addition.
Instances
| FuzzyAdd Integer Source # | This instance defines how |
Defined in Lessons.Lesson03 | |
data FireExtinguisher Source #
Records are usual ADTs which allow to name fields.
The FireExtinguisher type represents a simple data structure
describing a fire extinguisher. Each extinguisher has a capacity
and a type (FEType), which is imported from another module.
Records automatically provide accessors for record's fields.
Constructors
| FireExtinguisher | |
Instances
| Show FireExtinguisher Source # | |
Defined in Lessons.Lesson03 Methods showsPrec :: Int -> FireExtinguisher -> ShowS show :: FireExtinguisher -> String showList :: [FireExtinguisher] -> ShowS | |
fe :: FireExtinguisher Source #
This value represents a specific example of a fire extinguisher.
It has a capacity of 10 and is of type A.
Accessor example:
>>>capacity fe10
fe2 :: FireExtinguisher Source #
This example shows how to use record update syntax in Haskell.
It creates a new extinguisher based on fe, but with the capacity
changed to 100, while keeping all other fields the same.
cpcty :: FireExtinguisher -> Integer Source #
This function extracts the capacity field from a given
FireExtinguisher record. It demonstrates simple pattern matching
on a record structure (just like usual ADTs)
The Bucket data type defines another record-style structure.
It contains a single field, bucketCapacity, which stores how
much the bucket can hold. The 'deriving Show' clause again
provides a default string representation.
Constructors
| Bucket | |
Fields
| |
safeHead :: [a] -> Maybe a Source #
What if we do not have a value to return? Other languages have nulls
for that. The safeHead function retrieves the first element of a list
in a safe way. Normally, calling head on an empty list causes
a runtime error. To prevent that, this function returns a Maybe value:
'Just a' if the list has a first element, or Nothing if it is empty.
safeHeadDefault :: [a] -> a -> a Source #
The safeHeadDefault function is a practical extension of safeHead.
It takes a list and a default value. If the list is empty, it returns
the default value; otherwise, it returns the first element of the list.
Internally, it uses pattern matching on the result of safeHead
to decide which value to return.