From d5f380436a291a80f33cda2d13b903cf0545d659 Mon Sep 17 00:00:00 2001 From: 2weiEmu Date: Mon, 10 Feb 2025 15:54:28 +0100 Subject: [PATCH] added notes for functional --- .obsidian/workspace.json | 6 +- .../Functional Programming Introduction.md | 111 ++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 Functional Programming/Functional Programming Introduction.md diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index f925d0c..01b5468 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -13,7 +13,7 @@ "state": { "type": "markdown", "state": { - "file": "Ruxi.md", + "file": "Functional Programming/Functional Programming Introduction.md", "mode": "source", "source": false, "backlinks": true, @@ -28,7 +28,7 @@ } }, "icon": "lucide-file", - "title": "Ruxi" + "title": "Functional Programming Introduction" } } ] @@ -175,11 +175,11 @@ }, "active": "d417039fb31acf08", "lastOpenFiles": [ + "Ruxi.md", "Pasted image 20250207160807.png", "Documents Archive Organisational Notes.md", "Untitled.md", "Untitled", - "Ruxi.md", "System Overview and Links.md", "calendar.google.com.md", "Daily/27-01-2025.md", diff --git a/Functional Programming/Functional Programming Introduction.md b/Functional Programming/Functional Programming Introduction.md new file mode 100644 index 0000000..752f304 --- /dev/null +++ b/Functional Programming/Functional Programming Introduction.md @@ -0,0 +1,111 @@ +# Haskell Syntax +Math vs. Haskell +f(x) - f x +f(x,y) - f x y +f(g(x)) - f (g x) +f(x, g(y)) - f x (g y) +f(x)g(y) - f x * g y + +## Naming +Names of constructs start with a capital letter. +Names of functions and vars start with a small letter. +Concrete types start with a capital. +Type variables start with a small var. + +Keywords may not be used as names. + +Comments: -- (single line) +Multiline: {- Multiline would go in here -} +They can be nested + +# Conditionals +if ... then ... else ... + +```haskell +abs x = if x < 0 then -x else x +``` + +intermediate / subexpression using 'let' binding. They are immutable. + +```haskell +cylinderSurfaceArea r h = + let sideArea = 2 * pi * r * h + topArea = pi * r ^ 2 + in sideArea + 2 * topArea +``` + +## Function Definition +Each definition can have an optional type signature +A definition is one or more _clauses_ +Helper functions can be put in a `where` block + +## Where vs. let +- let defines vars before they are used, where defines them after +- let ... in ... can appear anywhere in an expression while where is always attached to a `clause` + +## The Layout Rule +Haskell is layout sensitive +Indentation of code matters + +Definitions at the same level of indentation belong to the same block + +Within a block all definitions must start at the exact same column. +blocks start with one of the keywords: where, let, case or do + +# Types in Haskell +a type is a name for a collection of values +Haskell syntax for typing: `True :: Bool` +You can write type annotations almost anywhere, and optional + + `Int` = 64 bit integer + `Integer` = any size integer + +## Pure Functions +a pure function can only return a value, or loop forever +it can't: +- modify a global var +- throw an exception +- read or write a file +- send a message over the network + +(a,b) is the type of tuples (x,y) where x :: a and y :: b. +Example: +(42, True) :: (Int, Bool) +('x', 'y', 'z') :: (Char, Char, Char) + +## Functions with several arguments +add1 :: (Int, Int) -> Int +add2 :: Int -> (Int -> Int) + +add1 takes as argument a tuple (x,y) and returns and Int +add2 takes as argument an Int and returns a function of type Int -> Int which can then be called with the next argument + +A function that returns another function is called a curried function after Haskell B. Curry. +```haskell +> :t add2 1 +Int -> Int +> (add2 1) 1 +2 +``` + +```haskell +Int -> Bool -> Char === Int -> (Bool -> Char) +``` + +function application associates to the left +`f x y == (f x) y` + +## List Type +[t] is the type of (singly linked lists) [x1, x2, ..., xn] with elements of type t +```haskell +[True, False] :: [Bool] +[1,2,3] :: Num a => [a] +[[1],[1,2],[1,2,3]] :: Num a => [[a]] +['a','b','c'] :: String +``` + +Why Num a? because technically, this is the broadest category that these fit in, because they could technically also be floats. +_this may have changed apparently idfk_ + + +`type String = [Char]` \ No newline at end of file -- 2.54.0