Ord t
type t must support ordering/comparison
Ord t
type t must support ordering/comparison
filter
function
An essential thing to know before trying to type in the above function is that Haskell delimits the scope of multi-line function definitions (and all multiline expressions) with indentation
Haskell uses indentation to know which lines belong to a block code
IO monad
a computation that can perform input/perform
() (called unitA type with exactly one value, (), used to indicate the absence of meaningful return value, similar to void in other languages. )
void in Java / C / TypeScript
Tail Call Optimisation
normal recursion creates a new stack frame for every recursive call. Tail recursion can be converted by the compiler into a loop, so it doesn't keep growing the stack
TCO does not means faster, its just for reducing stack usage
Exercise What other functions called in the JavaScript code generated for the above definition of main are curried? Why?
add x y = x + y
Currying allows a function to receive arguments one at a time, which enables partial application and makes functions eaiser to reuse and compose
Exercise If one didn’t happen to like the fact that function chaining with the $ operator reads right to left, how would one go about creating an operator that chains left to right? (Hint: infixl is a thing and you will need to make a slightly different apply function also).
operator called |>
x |> f === f x
opposite orientation of $
as $ uses apply f x = f x
x |> f becomes applyLeft x f = f x
Ans: infixl 0 applyLeft as |>
in Haskell, putting the argument after the function left-to-right unless operators change the grouping
replace every $ with brackets main = log $ show $ map fibs $ 1..10 equivalent to main = log (show (map fibs (1..10)))
map fibs (1..10) apply fibs to every number in the list
pattern matchingA mechanism in functional programming languages to check a value against a pattern and to deconstruct data. ,
fibs 0 = 1 ^ ^ ^ | | | | | result | pattern function
Haskell commonly uses pattern matching instead of writing if/else for cases where the input's structure or value determines what to do.
cannot till Dirt on an adjacent tile.
only can change the current Dirt that the farmer is stepping on
The game must then show the Farmer's current HP, SP, and inventory contents, as in the following example:
cannot fall below zero or exceed the maximum of 20
BaseStatistic helps to do this
REQ1 Heath points (HP) = hit point which is in Farmer constructor
GameEntity class is used to control the actor's statistic
unconscious(map) removes the Farmer from the map
At the start of each Farmer turn, the Farmer regenerates 1 SP before the action menu is displayed.
add in tick()
at 0 HP, they die.
this is check in isConscious() inside farmer's playTurn()
High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend on abstractions.
Depend on abstractions rather than concrete implementation
Liskov Substitution Principle
A child/subclass should be able to replace its parent/superclass without breaking the program's expected behaviour.