diff --git a/semester4/fmfp/code/00_syntax.hs b/semester4/fmfp/code/00_syntax.hs new file mode 100644 index 0000000..fefd062 --- /dev/null +++ b/semester4/fmfp/code/00_syntax.hs @@ -0,0 +1,11 @@ +-- Declaring a function. Naming using lowerCamelCase +-- Arguments separated by whitespace +myFunc :: Int -> Int -> Int +myFunc x y + | x > 0 = x + y + | x < 0 = -x + y + +-- On compile the above function is transformed like this: +-- TODO: Transform the template into correct version +myFuncXCompiled :: Int -> Int +myFuncXCompiled x = x diff --git a/semester4/fmfp/formal-methods-functional-programming-summary.pdf b/semester4/fmfp/formal-methods-functional-programming-summary.pdf index 440681d..70f8c2c 100644 Binary files a/semester4/fmfp/formal-methods-functional-programming-summary.pdf and b/semester4/fmfp/formal-methods-functional-programming-summary.pdf differ diff --git a/semester4/fmfp/formal-methods-functional-programming-summary.tex b/semester4/fmfp/formal-methods-functional-programming-summary.tex index b57be80..df0eb88 100644 --- a/semester4/fmfp/formal-methods-functional-programming-summary.tex +++ b/semester4/fmfp/formal-methods-functional-programming-summary.tex @@ -39,6 +39,10 @@ \printtoc{Aquamarine} +\newsection +\section{Haskell} +\input{parts/00_haskell/00_intro.tex} +\input{parts/00_haskell/01_syntax.tex} diff --git a/semester4/fmfp/parts/00_haskell/00_intro.tex b/semester4/fmfp/parts/00_haskell/00_intro.tex index e69de29..043bf00 100644 --- a/semester4/fmfp/parts/00_haskell/00_intro.tex +++ b/semester4/fmfp/parts/00_haskell/00_intro.tex @@ -0,0 +1,17 @@ +Haskell is a functional programming language. As such, its functions can be thought of as being similar to mathematical functions and as such are side-effect-free. + +Haskell's type system is very robust and an interesting topic to learn about. +The basic data types you already know from other programming languages are also present here. This includes all primitives like integers, floating point numbers, chars and booleans. + +Strings are handled similarly to how \texttt{C} does it, in that strings are char arrays. + +Arrays however are dynamic length in Haskell as opposed to many other statically typed programming languages. + +Since Haskell is an imperative language (i.e. you describe \textit{what} you want achieve) +as opposed to a declarative language (i.e. you describe \textit{how} you achieve what you want to achieve), there are no loops in Haskell, +as loops don't appear in mathematical formulas and functions either. +What we can do however is recursion and this is the main way of doing iterative work in Haskell. + +Additionally, Haskell features \textit{lazy evaluation} (i.e. statements are evaluated only as needed) as opposed to \textit{eager evaluation} (i.e. statemetns are evaluated immediately). + +In this course the \texttt{Glasgow Haskell Compiler}, short \texttt{ghc} is used. Installation is really easy (as long as you're on Linux) diff --git a/semester4/fmfp/parts/00_haskell/01_syntax.tex b/semester4/fmfp/parts/00_haskell/01_syntax.tex index e69de29..bbad742 100644 --- a/semester4/fmfp/parts/00_haskell/01_syntax.tex +++ b/semester4/fmfp/parts/00_haskell/01_syntax.tex @@ -0,0 +1,12 @@ +\subsection{The bad news: The syntax} +In short: It's quite bad, but you will get used to it and some of the (arguably) poor looking syntax choices will start to make more sense. + +You should use 2 space indents (yuck) and indents matter, just like in Python. + +We can use binary functions in infix or prefix notation, i.e. \texttt{x `mod` z} and \texttt{mod x z} are equivalent. + +For integers the following functions are available: Normal arithmetic operations \texttt{+, -, *, /, mod, abs}, as well as \texttt{\^} which is used for exponentiation. + +To use prefix notation on non-alphanumeric function names, wrap them in parenthesis like this: \texttt{(+) x z}. Using \texttt{+ x z} does not work. + +We can use the normal comparison operators that return a boolean on evaluation. \bi{Booleans} are \texttt{True} and \texttt{False} diff --git a/semester4/fmfp/parts/00_haskell/02_evalutation.tex b/semester4/fmfp/parts/00_haskell/02_evalutation.tex new file mode 100644 index 0000000..e69de29