V8 Flashcards
Algorithms and Functions
what are algorithms
- a list of a discrete number of steps that, when followed, will complete a certain task (or fail)
state diagrams
used to describe the behaviour pf a system
- visualisation of an algorithm
very similar to a flow chart
- state diagrams respond to actions/events
- flowcharts do not need explicit events
for/for each loop
- modelled very close to natural language
- > for(x in 1:ncol(M)) = for each x going from 1 to the umber of columns in M
while loop
- less natural bur more flexible
design pattern
- general reusable solution to a commonly occurring problem within a given context in software design
- not a finished design that can be transformed directly into source or machine code, is a template for how to solve a problem that can be used in many different situations
- to avoid reinventing the wheel
design patterns: some examples
- model view controller(MVC): used in web development
- monte carlo sampling: everything that might happen , used in genetics, economics, agriculture, etc.
- facade pattern - provide a stable call - interface
- scheduler - ways to schedule tasks in a certain time
recursion + example sum up 7 (calculates 1+2+3+4+5+6+7)
- a function calling itself
- a simple base case (or cases)
- rules that reduce all other cases toward the base case
example:
sumUp
important notes for recursion
- hand weird/unexpected input
- there is a recursion limit in R
- > set limit by : options(expressions = 500)
- every time we call our function : a new function call is created, internal variables need to be stored
- recursion invariant
sumUp if input negative then the ‘ summing up’ becomes infinite
- there is no stop for negative numbers
why use recursion
- in many programming languages - more efficient, cleaner code, often more in line to how people think about problems
indirect recursions
then can return different functions within each other
-> that makes them indirect - what the purpose of this is, i have no idea