Add Haskell main and curried functions pages.
This commit is contained in:
parent
9df113b477
commit
c56eb846c2
@ -18,6 +18,7 @@ const SoftwareMain = () => {
|
||||
<p>{getString('code_pages_intro')}</p>
|
||||
<ul>
|
||||
<li><a href={'/' + language + '/software/elm/'}>Elm</a></li>
|
||||
<li><a href={'/' + language + '/software/haskell/'}>Haskell</a></li>
|
||||
</ul>
|
||||
<hr />
|
||||
|
||||
|
||||
16
src/Pages/Software/haskell/HaskellPage.js
Normal file
16
src/Pages/Software/haskell/HaskellPage.js
Normal file
@ -0,0 +1,16 @@
|
||||
import React from 'react';
|
||||
import Breadcrumbs from '../../../UI/Breadcrumbs'
|
||||
import MarkdownPage from '../../markdownPage'
|
||||
|
||||
const HaskellPage = ({ mdPath }) => {
|
||||
return (
|
||||
<article className='main-page'>
|
||||
<Breadcrumbs separator=' > ' path="software/haskell">
|
||||
{['software', 'haskell']}
|
||||
</Breadcrumbs>
|
||||
<MarkdownPage md={mdPath} />
|
||||
</article>
|
||||
)
|
||||
}
|
||||
|
||||
export default HaskellPage;
|
||||
66
src/Pages/Software/haskell/en/curried-functions.md
Normal file
66
src/Pages/Software/haskell/en/curried-functions.md
Normal file
@ -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.
|
||||
5
src/Pages/Software/haskell/en/main.md
Normal file
5
src/Pages/Software/haskell/en/main.md
Normal file
@ -0,0 +1,5 @@
|
||||
### Haskell
|
||||
|
||||
I am learing functional programming and Haskell, these are my notes on the language.
|
||||
|
||||
* [Curried Functions](./curried-functions)
|
||||
66
src/Pages/Software/haskell/nl/curried-functions.md
Normal file
66
src/Pages/Software/haskell/nl/curried-functions.md
Normal file
@ -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.
|
||||
5
src/Pages/Software/haskell/nl/main.md
Normal file
5
src/Pages/Software/haskell/nl/main.md
Normal file
@ -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)
|
||||
@ -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 = <Route key={`${lang}-csharp-main`} exact path={`/${lang}/software/csharp/`} element={<CSharpPage mdPath={`Software/csharp/${lang}/main.md`} />} />
|
||||
const mainElm = <Route key={`${lang}-elm-main`} exact path={`/${lang}/software/elm/`} element={<ElmPage mdPath={`Software/elm/${lang}/main.md`} />} />
|
||||
const mainHaskell = <Route key={`${lang}-haskell-main`} exact path={`/${lang}/software/haskell/`} element={<HaskellPage mdPath={`Software/haskell/${lang}/main.md`} />} />
|
||||
|
||||
// Pages within C#.
|
||||
let md = ['strings', 'types'];
|
||||
@ -56,5 +58,13 @@ export const programmingLanguageRoutes = languages.map(lang => {
|
||||
return <Route key={`${lang}-${entry}`} exact path={path} element={<ElmPage mdPath={mdPath} />} />;
|
||||
});
|
||||
|
||||
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 <Route key={`${lang}-${entry}`} exact path={path} element={<HaskellPage mdPath={mdPath} />} />;
|
||||
});
|
||||
|
||||
return [mainCSharp, mainElm, mainHaskell, ...entriesCSharp, ...entriesElm, ...entriesHaskell];
|
||||
}).flat();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user