From c56eb846c260aca306fcab8d441098bc87d6c147 Mon Sep 17 00:00:00 2001 From: Kevin Matsubara Date: Sat, 15 Feb 2025 19:21:49 +0100 Subject: [PATCH] Add Haskell main and curried functions pages. --- src/Pages/Software/SoftwareMain.js | 1 + src/Pages/Software/haskell/HaskellPage.js | 16 +++++ .../Software/haskell/en/curried-functions.md | 66 +++++++++++++++++++ src/Pages/Software/haskell/en/main.md | 5 ++ .../Software/haskell/nl/curried-functions.md | 66 +++++++++++++++++++ src/Pages/Software/haskell/nl/main.md | 5 ++ src/Routers.js | 12 +++- 7 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 src/Pages/Software/haskell/HaskellPage.js create mode 100644 src/Pages/Software/haskell/en/curried-functions.md create mode 100644 src/Pages/Software/haskell/en/main.md create mode 100644 src/Pages/Software/haskell/nl/curried-functions.md create mode 100644 src/Pages/Software/haskell/nl/main.md diff --git a/src/Pages/Software/SoftwareMain.js b/src/Pages/Software/SoftwareMain.js index da23bc8..820f3e8 100644 --- a/src/Pages/Software/SoftwareMain.js +++ b/src/Pages/Software/SoftwareMain.js @@ -18,6 +18,7 @@ const SoftwareMain = () => {

{getString('code_pages_intro')}


diff --git a/src/Pages/Software/haskell/HaskellPage.js b/src/Pages/Software/haskell/HaskellPage.js new file mode 100644 index 0000000..2a93c92 --- /dev/null +++ b/src/Pages/Software/haskell/HaskellPage.js @@ -0,0 +1,16 @@ +import React from 'react'; +import Breadcrumbs from '../../../UI/Breadcrumbs' +import MarkdownPage from '../../markdownPage' + +const HaskellPage = ({ mdPath }) => { + return ( +
+ + {['software', 'haskell']} + + +
+ ) +} + +export default HaskellPage; \ No newline at end of file diff --git a/src/Pages/Software/haskell/en/curried-functions.md b/src/Pages/Software/haskell/en/curried-functions.md new file mode 100644 index 0000000..2955dc3 --- /dev/null +++ b/src/Pages/Software/haskell/en/curried-functions.md @@ -0,0 +1,66 @@ +## Curried functions + +A function can return another function. + +```haskell +add' :: Int -> (Int -> Int) +add' x y = x+y +``` + +Here, **add'** is a function that takes an **Int** for an argument and results in a function of type: **Int -> Int**. +The function definition takes an integer **x**, followed by an integer **y**, it can be [curried](https://en.wikipedia.org/wiki/Currying). + +```haskell +addThree = add' 3 -- This is now a function with type: Int -> Int +result = addThree 5 -- Evaluates to 8 +``` + +Another example, + +```haskell +mult :: Int -> (Int -> (Int -> Int)) +mult x y z = x*y*z +``` + +And is applied as following: + +```haskell +mult x y z +-- Means same as: +((mult x) y) z +``` + +When used, it is like: + +```haskell +multTwo = mult 2 -- This is now a function with type: Int -> (Int -> Int) +multTwoThree = multTwo 3 -- This is: Int -> Int +result = multTwoThree 4 -- Evaluates to 2 * 3 * 4 = 24 + +-- or just: + +result = mult 2 3 4 -- Also evaluates to 24 +``` + +**Partial application** is about using curried functions, applying only some arguments and getting back a new function. + +```haskell +double = mult 2 -- This is now a function with type: Int -> (Int -> Int) + +result = double 3 4 -- Evaluates to 2 * 3 * 4 = 24 + +quadruple = double 2 -- Now quadruple :: Int -> Int +result = quadruple 3 -- Evaluates to 2 * 2 * 3 = 12 +``` + +The arrow function **->** in Haskell types is assumed to associate from the right. + +```haskell +Int -> Int -> Int -> Int + +-- is: + +Int -> (Int -> (Int -> Int)) +``` + +So, unless tuples are required, all functions in Haskell with multiple arguments are actually defined as curried functions, with a way to reduce excessive parenthesis. diff --git a/src/Pages/Software/haskell/en/main.md b/src/Pages/Software/haskell/en/main.md new file mode 100644 index 0000000..ae3de64 --- /dev/null +++ b/src/Pages/Software/haskell/en/main.md @@ -0,0 +1,5 @@ +### Haskell + +I am learing functional programming and Haskell, these are my notes on the language. + +* [Curried Functions](./curried-functions) \ No newline at end of file diff --git a/src/Pages/Software/haskell/nl/curried-functions.md b/src/Pages/Software/haskell/nl/curried-functions.md new file mode 100644 index 0000000..77b55e2 --- /dev/null +++ b/src/Pages/Software/haskell/nl/curried-functions.md @@ -0,0 +1,66 @@ +## Curried functions + +Een functie kan een andere functie teruggeven. + +```haskell +add' :: Int -> (Int -> Int) +add' x y = x+y +``` + +Hier, is **add'** functie die een **Int** als argument neemt en resulteerd in een functie van het type: **Int -> Int**. +De functie definitie neemt een integer **x**, gevolgd door integer **y**, die kan worden [ge-curried](https://en.wikipedia.org/wiki/Currying). + +```haskell +addThree = add' 3 -- Dit is nu een functie met type: Int -> Int +result = addThree 5 -- Resulteerd in 8 +``` + +Nog een voorbeeld + +```haskell +mult :: Int -> (Int -> (Int -> Int)) +mult x y z = x*y*z +``` + +En toegepast: + +```haskell +mult x y z +-- Hetzelfde als: +((mult x) y) z +``` + +Als deze wordt gebruikt, kan dat zo: + +```haskell +multTwo = mult 2 -- Dit is nu een functie met type: Int -> (Int -> Int) +multTwoThree = multTwo 3 -- Dit is is: Int -> Int +result = multTwoThree 4 -- Resulteerd in: 2 * 3 * 4 = 24 + +-- of gewoon: + +result = mult 2 3 4 -- Resulteerd ook in 24 +``` + +**Partial application** is het gebruiken van curried functies, door enkele argumenten te geven en een functie terug te krijgen. + +```haskell +double = mult 2 -- Dit is nu een functie met type: Int -> (Int -> Int) + +result = double 3 4 -- Resulteerd in: 2 * 3 * 4 = 24 + +quadruple = double 2 -- quadruple :: Int -> Int +result = quadruple 3 -- Resulteerd in: 2 * 2 * 3 = 12 +``` + +De pijl **->** in Haskell types associeerd vanaf rechts. + +```haskell +Int -> Int -> Int -> Int + +-- is: + +Int -> (Int -> (Int -> Int)) +``` + +Tenzij tuples nodig zijn, zijn alle functies in Haskell met meerdere argumenten eigenlijk curried functies, met een manier om de vele haakjes weg te kunnen laten. diff --git a/src/Pages/Software/haskell/nl/main.md b/src/Pages/Software/haskell/nl/main.md new file mode 100644 index 0000000..1498039 --- /dev/null +++ b/src/Pages/Software/haskell/nl/main.md @@ -0,0 +1,5 @@ +### Haskell + +I ben functioneel programmeren en Haskell aan het leren, dit zijn mijn notities van deze taal. + +* [Curried Functions](./curried-functions) \ No newline at end of file diff --git a/src/Routers.js b/src/Routers.js index 475c6d2..97bff5e 100644 --- a/src/Routers.js +++ b/src/Routers.js @@ -5,6 +5,7 @@ import JapanesePage from './Pages/Japan/JapanesePage'; import BlogPage from './Pages/Plamo/BlogPage'; import CSharpPage from './Pages/Software/csharp/CSharpPage'; import ElmPage from './Pages/Software/elm/ElmPage'; +import HaskellPage from './Pages/Software/haskell/HaskellPage'; // Japan export const japaneseRoutes = languages.map(lang => { @@ -39,6 +40,7 @@ export const programmingLanguageRoutes = languages.map(lang => { // Main page. const mainCSharp = } /> const mainElm = } /> + const mainHaskell = } /> // Pages within C#. let md = ['strings', 'types']; @@ -56,5 +58,13 @@ export const programmingLanguageRoutes = languages.map(lang => { return } />; }); - return [mainCSharp, mainElm, ...entriesCSharp, ...entriesElm]; + // Pages within Haskell. + md = ['curried-functions']; + const entriesHaskell = md.map(entry => { + const path = `/${lang}/software/haskell/${entry}`; + const mdPath = `Software/haskell/${lang}/${entry}.md`; + return } />; + }); + + return [mainCSharp, mainElm, mainHaskell, ...entriesCSharp, ...entriesElm, ...entriesHaskell]; }).flat();