added doi
This commit is contained in:
103
dist/Coding/Haskell/Code Snippets/Monoid.html
vendored
103
dist/Coding/Haskell/Code Snippets/Monoid.html
vendored
@ -2,7 +2,7 @@
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||||
|
||||
<meta charset="utf-8">
|
||||
<meta name="generator" content="quarto-1.7.23">
|
||||
<meta name="generator" content="quarto-1.8.17">
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||||
|
||||
@ -68,17 +68,19 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<meta name="quarto:offset" content="../../../">
|
||||
<script src="../../../site_libs/quarto-html/quarto.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/tabsets/tabsets.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/axe/axe-check.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/popper.min.js"></script>
|
||||
<script src="../../../site_libs/quarto-html/tippy.umd.min.js"></script>
|
||||
<script src="../../../site_libs/quarto-html/anchor.min.js"></script>
|
||||
<link href="../../../site_libs/quarto-html/tippy.css" rel="stylesheet">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-dark-2c84ecb840a13f4c7993f9e5648f0c14.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-6cf5824034cebd0380a5b9c74c43f006.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-ff1c8a91a872873ab9706226229f9e30.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-dark-04b38c9dba5d7c0f1fe2b621b39e7e62.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-ff1c8a91a872873ab9706226229f9e30.css" rel="stylesheet" class="quarto-color-scheme-extra" id="quarto-text-highlighting-styles">
|
||||
<script src="../../../site_libs/bootstrap/bootstrap.min.js"></script>
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-ec71cb1e120c0dd41819aca960e74e38.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-dark-716ed94a23403968eaa6fe981e0cbf91.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-ec71cb1e120c0dd41819aca960e74e38.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-9b56b6674ace31b1957ea5f5ef42286f.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-dark-3c8015a3c2181875b3e8a350495bd83c.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-9b56b6674ace31b1957ea5f5ef42286f.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
|
||||
<script id="quarto-search-options" type="application/json">{
|
||||
"location": "navbar",
|
||||
"copy-button": false,
|
||||
@ -131,7 +133,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
toggleBodyColorMode(bsSheetEl);
|
||||
}
|
||||
}
|
||||
window.setColorSchemeToggle = (alternate) => {
|
||||
const setColorSchemeToggle = (alternate) => {
|
||||
const toggles = window.document.querySelectorAll('.quarto-color-scheme-toggle');
|
||||
for (let i=0; i < toggles.length; i++) {
|
||||
const toggle = toggles[i];
|
||||
@ -164,7 +166,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
}
|
||||
manageTransitions('#quarto-margin-sidebar .nav-link', true);
|
||||
// Switch the toggles
|
||||
window.setColorSchemeToggle(alternate)
|
||||
setColorSchemeToggle(alternate)
|
||||
// Hack to workaround the fact that safari doesn't
|
||||
// properly recolor the scrollbar when toggling (#1455)
|
||||
if (navigator.userAgent.indexOf('Safari') > 0 && navigator.userAgent.indexOf('Chrome') == -1) {
|
||||
@ -204,7 +206,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
const isFileUrl = () => {
|
||||
return window.location.protocol === 'file:';
|
||||
}
|
||||
window.hasAlternateSentinel = () => {
|
||||
const hasAlternateSentinel = () => {
|
||||
let styleSentinel = getColorSchemeSentinel();
|
||||
if (styleSentinel !== null) {
|
||||
return styleSentinel === "alternate";
|
||||
@ -232,7 +234,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
const baseTheme = document.querySelector('#giscus-base-theme')?.value ?? 'light';
|
||||
const alternateTheme = document.querySelector('#giscus-alt-theme')?.value ?? 'dark';
|
||||
let newTheme = '';
|
||||
if(darkModeDefault) {
|
||||
if(authorPrefersDark) {
|
||||
newTheme = isAlternate ? baseTheme : alternateTheme;
|
||||
} else {
|
||||
newTheme = isAlternate ? alternateTheme : baseTheme;
|
||||
@ -255,17 +257,20 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
changeGiscusTheme();
|
||||
}
|
||||
};
|
||||
const authorPrefersDark = false;
|
||||
const queryPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const darkModeDefault = queryPrefersDark.matches;
|
||||
document.querySelector('link.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
document.querySelector('link#quarto-text-highlighting-styles.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
document.querySelector('link#quarto-bootstrap.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
let localAlternateSentinel = darkModeDefault ? 'alternate' : 'default';
|
||||
// Dark / light mode switch
|
||||
window.quartoToggleColorScheme = () => {
|
||||
// Read the current dark / light value
|
||||
let toAlternate = !window.hasAlternateSentinel();
|
||||
let toAlternate = !hasAlternateSentinel();
|
||||
toggleColorMode(toAlternate);
|
||||
setStyleSentinel(toAlternate);
|
||||
toggleGiscusIfUsed(toAlternate, darkModeDefault);
|
||||
window.dispatchEvent(new Event('resize'));
|
||||
};
|
||||
queryPrefersDark.addEventListener("change", e => {
|
||||
if(window.localStorage.getItem("quarto-color-scheme") !== null)
|
||||
@ -276,7 +281,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
toggleGiscusIfUsed(alternate, darkModeDefault);
|
||||
});
|
||||
// Switch to dark mode if need be
|
||||
if (window.hasAlternateSentinel()) {
|
||||
if (hasAlternateSentinel()) {
|
||||
toggleColorMode(true);
|
||||
} else {
|
||||
toggleColorMode(false);
|
||||
@ -288,6 +293,8 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<nav class="navbar navbar-expand-lg " data-bs-theme="dark">
|
||||
<div class="navbar-container container-fluid">
|
||||
<div class="navbar-brand-container mx-auto">
|
||||
<a href="../../../index.html" class="navbar-brand navbar-brand-logo">
|
||||
</a>
|
||||
<a class="navbar-brand" href="../../../index.html">
|
||||
<span class="navbar-title">Nicole Dresselhaus</span>
|
||||
</a>
|
||||
@ -299,12 +306,12 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<div class="collapse navbar-collapse" id="navbarCollapse">
|
||||
<ul class="navbar-nav navbar-nav-scroll me-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="../../../index.html"> <i class="bi bi-house" role="img">
|
||||
<a class="nav-link active" href="../../../index.html" aria-current="page"> <i class="bi bi-house" role="img">
|
||||
</i>
|
||||
<span class="menu-text">Home</span></a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" href="../../../About/index.html" aria-current="page"> <i class="bi bi-file-person" role="img">
|
||||
<a class="nav-link" href="../../../About/index.html"> <i class="bi bi-file-person" role="img">
|
||||
</i>
|
||||
<span class="menu-text">About</span></a>
|
||||
</li>
|
||||
@ -342,6 +349,10 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
|
||||
<!-- sidebar -->
|
||||
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation docked overflow-auto">
|
||||
<div class="pt-lg-2 mt-2 text-left sidebar-header">
|
||||
<a href="../../../index.html" class="sidebar-logo-link">
|
||||
</a>
|
||||
</div>
|
||||
<div class="sidebar-menu-container">
|
||||
<ul class="list-unstyled mt-1">
|
||||
<li class="sidebar-item sidebar-item-section">
|
||||
@ -385,12 +396,6 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<a href="../../../Writing/Obsidian-RAG.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text">RAG für eine Obsidian-Wissensdatenbank: Technische Ansätze</span></a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="sidebar-item">
|
||||
<div class="sidebar-item-container">
|
||||
<a href="../../../Obsidian-Articles/Artikelbewertung durch LLM.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text">Artikelbewertung durch ein Large Language Model (LLM) - Ein Beispiel aus der Praxis</span></a>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
@ -663,7 +668,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
|
||||
|
||||
<p>Stellen wir uns vor, dass wir eine Funktion schreiben, die einen String bekommt (mehrere Lines mit ACSII-Text) und dieses Wort-für-Wort rückwärts ausgeben soll. Das ist ein einfacher Einzeiler:</p>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">module</span> <span class="dt">Main</span> <span class="kw">where</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">module</span> <span class="dt">Main</span> <span class="kw">where</span></span>
|
||||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">System.Environment</span> (getArgs)</span>
|
||||
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.Monoid</span> (mconcat)</span>
|
||||
@ -671,30 +676,30 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>main <span class="ot">=</span> <span class="kw">do</span></span>
|
||||
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> ls <span class="ot"><-</span> <span class="fu">readFile</span> <span class="op">=<<</span> <span class="fu">head</span> <span class="op"><$></span> getArgs</span>
|
||||
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mconcat</span> <span class="op"><$></span> <span class="fu">mapM</span> (<span class="fu">putStrLn</span> <span class="op">.</span> <span class="fu">unwords</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">words</span>) (<span class="fu">lines</span> ls) <span class="co">--die eigentliche Funktion, ls ist das argument.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mconcat</span> <span class="op"><$></span> <span class="fu">mapM</span> (<span class="fu">putStrLn</span> <span class="op">.</span> <span class="fu">unwords</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">words</span>) (<span class="fu">lines</span> ls) <span class="co">--die eigentliche Funktion, ls ist das argument.</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Was passiert hier an Vodoo? Und was machen die ganzen wilden Zeichen da?</p>
|
||||
<p>Gehen wir die Main zeilenweise durch: Wir lesen die Datei, die im ersten Kommandozeilen-Argument gegeben wird. getArgs hat folgende Signatur:</p>
|
||||
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ot">getArgs ::</span> <span class="dt">IO</span> [<span class="dt">String</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ot">getArgs ::</span> <span class="dt">IO</span> [<span class="dt">String</span>]</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Wir bekommen als eine Liste der Argumente. Wir wollen nur das erste. Also machen wir head getArgs. Allerdings fliegt uns dann ein Fehler. head sieht nämlich so aus:</p>
|
||||
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span><span class="ot"> ::</span> [a] <span class="ot">-></span> a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span><span class="ot"> ::</span> [a] <span class="ot">-></span> a</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Irgendwie müssen wird as <strong>in</strong> das IO bekommen. Hierzu gibt es fmap. Somit ist</p>
|
||||
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">fmap</span><span class="ot"> head ::</span> <span class="dt">IO</span> [a] <span class="ot">-></span> <span class="dt">IO</span> a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">fmap</span><span class="ot"> head ::</span> <span class="dt">IO</span> [a] <span class="ot">-></span> <span class="dt">IO</span> a</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Ein inline-Alias (um die Funktion links und das Argument rechts zu schreiben und sich ne Menge Klammern zu sparen) ist <$>. Somit ist schlussendlich der Inhalt der Datei aus dem ersten Argument (lazy) in ls.</p>
|
||||
<p>Eine andere Möglichkeit sich das (in diesem Fall) zu merken, bzw. drauf zu kommen ist, dass [] AUCH ein Funktor (sogar eine Monade) ist. Man könnte das also auch so schreiben:</p>
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span><span class="ot"> ::</span> [] a <span class="ot">-></span> a</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span><span class="ot"> ::</span> [] a <span class="ot">-></span> a</span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span><span class="ot"> ::</span> <span class="dt">Functor</span> f <span class="ot">=></span> [] (f a) <span class="ot">-></span> f a <span class="co">-- das "a" geschickt ersetzt zur Verdeutlichung</span></span>
|
||||
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="ot">getArgs ::</span> <span class="dt">IO</span> [] <span class="dt">String</span></span>
|
||||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="fu">fmap</span><span class="ot"> head ::</span> <span class="dt">Functor</span> f <span class="ot">=></span> f [] a <span class="ot">-></span> f a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="fu">fmap</span><span class="ot"> head ::</span> <span class="dt">Functor</span> f <span class="ot">=></span> f [] a <span class="ot">-></span> f a</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>fmap “packt” die Funktion quasi 1 Umgebung (Funktor, Monade, ..) weiter rein - Sei es nun in Maybe, Either oder irgendwas anderes.</p>
|
||||
<p>Alternatives (ausführliches) Beispiel am Ende.</p>
|
||||
<p>Wenn wir uns die Signatur ansehen, dann haben wir nun</p>
|
||||
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span> <span class="op"><$></span><span class="ot"> getArgs ::</span> <span class="dt">IO</span> <span class="dt">String</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span> <span class="op"><$></span><span class="ot"> getArgs ::</span> <span class="dt">IO</span> <span class="dt">String</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>readFile will aber nun ein String haben. Man kann nun</p>
|
||||
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>f <span class="ot"><-</span> <span class="fu">head</span> <span class="op"><$></span> getArgs</span>
|
||||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>ls <span class="ot"><-</span> <span class="fu">readFile</span> f</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>f <span class="ot"><-</span> <span class="fu">head</span> <span class="op"><$></span> getArgs</span>
|
||||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>ls <span class="ot"><-</span> <span class="fu">readFile</span> f</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>kann man auch “inline” mit =<< die Sachen “auspacken”.</p>
|
||||
<p>Die 2. Zeile lesen wir nun einfach “von hinten”, wie man das meistens tun sollte. Hier ist ein</p>
|
||||
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span><span class="ot"> ls ::</span> [<span class="dt">String</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span><span class="ot"> ls ::</span> [<span class="dt">String</span>]</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>was uns den Inhalt der Datei zeilenweise gibt. Mit jeder Zeile möchten wir nun folgendes machen:</p>
|
||||
<ol type="1">
|
||||
<li>nach Wörtern trennen (words)</li>
|
||||
@ -703,42 +708,42 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<li>diese Zeile ausgeben (putStrLn)</li>
|
||||
</ol>
|
||||
<p>Wenn wir uns die Signatur ansehen:</p>
|
||||
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>(<span class="fu">putStrLn</span> <span class="op">.</span> <span class="fu">unwords</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">words</span>)<span class="ot"> ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">IO</span> ()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>(<span class="fu">putStrLn</span> <span class="op">.</span> <span class="fu">unwords</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">words</span>)<span class="ot"> ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">IO</span> ()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Das mag im ersten Moment verwirren, daher noch die Signaturen der Einzelfunktionen:</p>
|
||||
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">words</span><span class="ot"> ::</span> <span class="dt">String</span> <span class="ot">-></span> [<span class="dt">String</span>]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">words</span><span class="ot"> ::</span> <span class="dt">String</span> <span class="ot">-></span> [<span class="dt">String</span>]</span>
|
||||
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="fu">reverse</span><span class="ot"> ::</span> [a] <span class="ot">-></span> [a]</span>
|
||||
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="fu">unwords</span><span class="ot"> ::</span> [<span class="dt">String</span>] <span class="ot">-></span> <span class="dt">String</span></span>
|
||||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="fu">putStrLn</span><span class="ot"> ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">IO</span> ()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="fu">putStrLn</span><span class="ot"> ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">IO</span> ()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Da wir am Ende in der IO-Monade landen müssen wir das auf unsere Zeilen mit mapM statt map anwenden. Dies sorgt auch dafür, dass die Liste der reihe nach durchgegangen wird. mapM mit unserer Funktion schaut dann so aus:</p>
|
||||
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mapM</span> (<span class="fu">putStrLn</span> <span class="op">.</span> <span class="fu">unwords</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">words</span>)<span class="ot"> ::</span> [<span class="dt">String</span>] <span class="ot">-></span> [<span class="dt">IO</span> ()]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mapM</span> (<span class="fu">putStrLn</span> <span class="op">.</span> <span class="fu">unwords</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">words</span>)<span class="ot"> ::</span> [<span class="dt">String</span>] <span class="ot">-></span> [<span class="dt">IO</span> ()]</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>eek! Das [IO ()] sieht ekelig aus. Wir haben eine Liste von IO-gar nichts. Das können wir eigentlich entsorgen. Da wir innerhalb der main-Funktion in einer IO-Monade sind, wollen wir IO () anstatt [IO ()] zurück haben.</p>
|
||||
<p>Wenn wir uns jetzt erinnern, dass [] auch nur eine Monade ist und dass jede Monade ein Monoid ist, dann ist die Lösung einfach. Monoide haben eine “append”-funktion (mappend oder (<>) genannt). Wenn wir “nichts” an “nichts” anhängen, dann erhalten wir …. <em>Trommelwirbel</em> “nichts”! Wir müssen die [IO ()]-Liste also “nur noch” mit mappend falten. Hierzu gibt es schon eine vorgefertigte Funktion:</p>
|
||||
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mconcat</span><span class="ot"> ::</span> [a] <span class="ot">-></span> a</span>
|
||||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mconcat</span> <span class="ot">=</span> <span class="fu">foldr</span> <span class="fu">mappend</span> <span class="fu">mempty</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mconcat</span><span class="ot"> ::</span> [a] <span class="ot">-></span> a</span>
|
||||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mconcat</span> <span class="ot">=</span> <span class="fu">foldr</span> <span class="fu">mappend</span> <span class="fu">mempty</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Was genau die gewünschte Faltung macht. Wir müssen nun wieder fmap nehmen, da wir die Liste selbst falten wollen - und nicht map, welches auf den IO () innerhalb der Liste arbeiten würde. Durch die Faltung fällt die Liste nun auf IO () zusammen.</p>
|
||||
<p>Viel Voodoo in wenig Code, aber wenn man sich dran gewöhnt hat, sind Monaden in Monaden auch nicht schlimm. Man muss sich immer nur richtig “rein” fmap’en.</p>
|
||||
<hr>
|
||||
<p>Kleinen Tipp gab es noch: mapM_ macht genau das, was oben mit mconcat erreicht werden sollte. Somit kann man auch</p>
|
||||
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mapM_</span> (<span class="fu">putStrLn</span> <span class="op">.</span> <span class="fu">unwords</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">words</span>) (<span class="fu">lines</span> ls)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mapM_</span> (<span class="fu">putStrLn</span> <span class="op">.</span> <span class="fu">unwords</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">words</span>) (<span class="fu">lines</span> ls)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>schreiben. Ich hab es aber mal wegen der klarheit oben so gelassen.</p>
|
||||
<section id="alternatives-fmap-beispiel" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="alternatives-fmap-beispiel">Alternatives fmap-Beispiel</h2>
|
||||
<p>Nehmen wir als alternatives Beispiel mal an:</p>
|
||||
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="ot">a ::</span> <span class="dt">IO</span> <span class="dt">Maybe</span> <span class="dt">State</span> t</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="ot">a ::</span> <span class="dt">IO</span> <span class="dt">Maybe</span> <span class="dt">State</span> t</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Um Funktionen vom Typ</p>
|
||||
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="ot">f ::</span> <span class="dt">IO</span> a <span class="ot">-></span> <span class="dt">IO</span> a</span>
|
||||
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>f a <span class="co">-- valide</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="ot">f ::</span> <span class="dt">IO</span> a <span class="ot">-></span> <span class="dt">IO</span> a</span>
|
||||
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>f a <span class="co">-- valide</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>zu nehmen, brauchen wir nichts machen. Bei</p>
|
||||
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="ot">f' ::</span> <span class="dt">Maybe</span> a <span class="ot">-></span> <span class="dt">Maybe</span> a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="ot">f' ::</span> <span class="dt">Maybe</span> a <span class="ot">-></span> <span class="dt">Maybe</span> a</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>brauchen wir 1 fmap, also ein</p>
|
||||
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>f' a <span class="co">-- error</span></span>
|
||||
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>f' <span class="op"><$></span> a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>f' a <span class="co">-- error</span></span>
|
||||
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>f' <span class="op"><$></span> a</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>um eine Funktion</p>
|
||||
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="ot">f'' ::</span> <span class="dt">State</span> t <span class="ot">-></span> <span class="dt">State</span> t</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="ot">f'' ::</span> <span class="dt">State</span> t <span class="ot">-></span> <span class="dt">State</span> t</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>zu benutzen folglich:</p>
|
||||
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>f'' a <span class="co">-- error</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>f'' a <span class="co">-- error</span></span>
|
||||
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>f'' <span class="op"><$></span> a <span class="co">-- error</span></span>
|
||||
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">fmap</span> f'' <span class="op"><$></span> a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">fmap</span> f'' <span class="op"><$></span> a</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
|
||||
|
||||
</section>
|
||||
@ -758,7 +763,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
a.appendChild(i);
|
||||
window.document.body.appendChild(a);
|
||||
}
|
||||
window.setColorSchemeToggle(window.hasAlternateSentinel())
|
||||
setColorSchemeToggle(hasAlternateSentinel())
|
||||
const icon = "";
|
||||
const anchorJS = new window.AnchorJS();
|
||||
anchorJS.options = {
|
||||
|
91
dist/Coding/Haskell/Code Snippets/Morphisms.html
vendored
91
dist/Coding/Haskell/Code Snippets/Morphisms.html
vendored
@ -2,7 +2,7 @@
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||||
|
||||
<meta charset="utf-8">
|
||||
<meta name="generator" content="quarto-1.7.23">
|
||||
<meta name="generator" content="quarto-1.8.17">
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||||
|
||||
@ -68,17 +68,19 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<meta name="quarto:offset" content="../../../">
|
||||
<script src="../../../site_libs/quarto-html/quarto.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/tabsets/tabsets.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/axe/axe-check.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/popper.min.js"></script>
|
||||
<script src="../../../site_libs/quarto-html/tippy.umd.min.js"></script>
|
||||
<script src="../../../site_libs/quarto-html/anchor.min.js"></script>
|
||||
<link href="../../../site_libs/quarto-html/tippy.css" rel="stylesheet">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-dark-2c84ecb840a13f4c7993f9e5648f0c14.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-6cf5824034cebd0380a5b9c74c43f006.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-ff1c8a91a872873ab9706226229f9e30.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-dark-04b38c9dba5d7c0f1fe2b621b39e7e62.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-ff1c8a91a872873ab9706226229f9e30.css" rel="stylesheet" class="quarto-color-scheme-extra" id="quarto-text-highlighting-styles">
|
||||
<script src="../../../site_libs/bootstrap/bootstrap.min.js"></script>
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-ec71cb1e120c0dd41819aca960e74e38.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-dark-716ed94a23403968eaa6fe981e0cbf91.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-ec71cb1e120c0dd41819aca960e74e38.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-9b56b6674ace31b1957ea5f5ef42286f.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-dark-3c8015a3c2181875b3e8a350495bd83c.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-9b56b6674ace31b1957ea5f5ef42286f.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
|
||||
<script id="quarto-search-options" type="application/json">{
|
||||
"location": "navbar",
|
||||
"copy-button": false,
|
||||
@ -133,7 +135,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
toggleBodyColorMode(bsSheetEl);
|
||||
}
|
||||
}
|
||||
window.setColorSchemeToggle = (alternate) => {
|
||||
const setColorSchemeToggle = (alternate) => {
|
||||
const toggles = window.document.querySelectorAll('.quarto-color-scheme-toggle');
|
||||
for (let i=0; i < toggles.length; i++) {
|
||||
const toggle = toggles[i];
|
||||
@ -166,7 +168,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
}
|
||||
manageTransitions('#quarto-margin-sidebar .nav-link', true);
|
||||
// Switch the toggles
|
||||
window.setColorSchemeToggle(alternate)
|
||||
setColorSchemeToggle(alternate)
|
||||
// Hack to workaround the fact that safari doesn't
|
||||
// properly recolor the scrollbar when toggling (#1455)
|
||||
if (navigator.userAgent.indexOf('Safari') > 0 && navigator.userAgent.indexOf('Chrome') == -1) {
|
||||
@ -206,7 +208,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
const isFileUrl = () => {
|
||||
return window.location.protocol === 'file:';
|
||||
}
|
||||
window.hasAlternateSentinel = () => {
|
||||
const hasAlternateSentinel = () => {
|
||||
let styleSentinel = getColorSchemeSentinel();
|
||||
if (styleSentinel !== null) {
|
||||
return styleSentinel === "alternate";
|
||||
@ -234,7 +236,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
const baseTheme = document.querySelector('#giscus-base-theme')?.value ?? 'light';
|
||||
const alternateTheme = document.querySelector('#giscus-alt-theme')?.value ?? 'dark';
|
||||
let newTheme = '';
|
||||
if(darkModeDefault) {
|
||||
if(authorPrefersDark) {
|
||||
newTheme = isAlternate ? baseTheme : alternateTheme;
|
||||
} else {
|
||||
newTheme = isAlternate ? alternateTheme : baseTheme;
|
||||
@ -257,17 +259,20 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
changeGiscusTheme();
|
||||
}
|
||||
};
|
||||
const authorPrefersDark = false;
|
||||
const queryPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const darkModeDefault = queryPrefersDark.matches;
|
||||
document.querySelector('link.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
document.querySelector('link#quarto-text-highlighting-styles.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
document.querySelector('link#quarto-bootstrap.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
let localAlternateSentinel = darkModeDefault ? 'alternate' : 'default';
|
||||
// Dark / light mode switch
|
||||
window.quartoToggleColorScheme = () => {
|
||||
// Read the current dark / light value
|
||||
let toAlternate = !window.hasAlternateSentinel();
|
||||
let toAlternate = !hasAlternateSentinel();
|
||||
toggleColorMode(toAlternate);
|
||||
setStyleSentinel(toAlternate);
|
||||
toggleGiscusIfUsed(toAlternate, darkModeDefault);
|
||||
window.dispatchEvent(new Event('resize'));
|
||||
};
|
||||
queryPrefersDark.addEventListener("change", e => {
|
||||
if(window.localStorage.getItem("quarto-color-scheme") !== null)
|
||||
@ -278,7 +283,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
toggleGiscusIfUsed(alternate, darkModeDefault);
|
||||
});
|
||||
// Switch to dark mode if need be
|
||||
if (window.hasAlternateSentinel()) {
|
||||
if (hasAlternateSentinel()) {
|
||||
toggleColorMode(true);
|
||||
} else {
|
||||
toggleColorMode(false);
|
||||
@ -290,6 +295,8 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
<nav class="navbar navbar-expand-lg " data-bs-theme="dark">
|
||||
<div class="navbar-container container-fluid">
|
||||
<div class="navbar-brand-container mx-auto">
|
||||
<a href="../../../index.html" class="navbar-brand navbar-brand-logo">
|
||||
</a>
|
||||
<a class="navbar-brand" href="../../../index.html">
|
||||
<span class="navbar-title">Nicole Dresselhaus</span>
|
||||
</a>
|
||||
@ -301,12 +308,12 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
<div class="collapse navbar-collapse" id="navbarCollapse">
|
||||
<ul class="navbar-nav navbar-nav-scroll me-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="../../../index.html"> <i class="bi bi-house" role="img">
|
||||
<a class="nav-link active" href="../../../index.html" aria-current="page"> <i class="bi bi-house" role="img">
|
||||
</i>
|
||||
<span class="menu-text">Home</span></a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" href="../../../About/index.html" aria-current="page"> <i class="bi bi-file-person" role="img">
|
||||
<a class="nav-link" href="../../../About/index.html"> <i class="bi bi-file-person" role="img">
|
||||
</i>
|
||||
<span class="menu-text">About</span></a>
|
||||
</li>
|
||||
@ -344,6 +351,10 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
|
||||
<!-- sidebar -->
|
||||
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation docked overflow-auto">
|
||||
<div class="pt-lg-2 mt-2 text-left sidebar-header">
|
||||
<a href="../../../index.html" class="sidebar-logo-link">
|
||||
</a>
|
||||
</div>
|
||||
<div class="sidebar-menu-container">
|
||||
<ul class="list-unstyled mt-1">
|
||||
<li class="sidebar-item sidebar-item-section">
|
||||
@ -387,12 +398,6 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
<a href="../../../Writing/Obsidian-RAG.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text">RAG für eine Obsidian-Wissensdatenbank: Technische Ansätze</span></a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="sidebar-item">
|
||||
<div class="sidebar-item-container">
|
||||
<a href="../../../Obsidian-Articles/Artikelbewertung durch LLM.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text">Artikelbewertung durch ein Large Language Model (LLM) - Ein Beispiel aus der Praxis</span></a>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
@ -679,45 +684,45 @@ Note
|
||||
<p>For those who need a refresher: MergeSort works by creating a balanced binary tree from the input list and directly collapsing it back into itself while treating the children as sorted lists and merging these with an O(n) algorithm.</p>
|
||||
<hr>
|
||||
<p>First the usual prelude:</p>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# LANGUAGE DeriveFunctor #-}</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# LANGUAGE DeriveFunctor #-}</span></span>
|
||||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# LANGUAGE TypeFamilies #-}</span></span>
|
||||
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.Functor.Foldable</span></span>
|
||||
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.List</span> (splitAt, unfoldr)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.List</span> (splitAt, unfoldr)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>We will use a binary tree like this. Note that there is no explicit recursion used, but <code>NodeF</code> has two <em>holes</em>. These will eventually filled later.</p>
|
||||
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">TreeF</span> c f <span class="ot">=</span> <span class="dt">EmptyF</span> <span class="op">|</span> <span class="dt">LeafF</span> c <span class="op">|</span> <span class="dt">NodeF</span> f f</span>
|
||||
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Functor</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">TreeF</span> c f <span class="ot">=</span> <span class="dt">EmptyF</span> <span class="op">|</span> <span class="dt">LeafF</span> c <span class="op">|</span> <span class="dt">NodeF</span> f f</span>
|
||||
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Functor</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>Aside: We could use this as a <em>normal</em> binary tree by wrapping it in <code>Fix</code>: <code>type Tree a = Fix (TreeF a)</code> But this would require us to write our tree like <code>Fix (NodeF (Fix (LeafF 'l')) (Fix (LeafF 'r')))</code> which would get tedious fast. Luckily Edward build a much better way to do this into <em>recursion-schemes</em>. I will touch on this later.</p>
|
||||
<hr>
|
||||
<p>Without further ado we start to write a Coalgebra, which in my book is just a scary name for “function that is used to construct datastructures”.</p>
|
||||
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ot">unflatten ::</span> [a] <span class="ot">-></span> <span class="dt">TreeF</span> a [a]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ot">unflatten ::</span> [a] <span class="ot">-></span> <span class="dt">TreeF</span> a [a]</span>
|
||||
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>unflatten ( []) <span class="ot">=</span> <span class="dt">EmptyF</span></span>
|
||||
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>unflatten (x<span class="op">:</span>[]) <span class="ot">=</span> <span class="dt">LeafF</span> x</span>
|
||||
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>unflatten ( xs) <span class="ot">=</span> <span class="dt">NodeF</span> l r <span class="kw">where</span> (l,r) <span class="ot">=</span> <span class="fu">splitAt</span> (<span class="fu">length</span> xs <span class="ot">`div`</span> <span class="dv">2</span>) xs</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>unflatten ( xs) <span class="ot">=</span> <span class="dt">NodeF</span> l r <span class="kw">where</span> (l,r) <span class="ot">=</span> <span class="fu">splitAt</span> (<span class="fu">length</span> xs <span class="ot">`div`</span> <span class="dv">2</span>) xs</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>From the type signature it’s immediately obvious, that we take a list of ’a’s and use it to create a part of our tree.</p>
|
||||
<p>The nice thing is that due to the fact that we haven’t commited to a type in our tree nodes we can just put lists in there.</p>
|
||||
<hr>
|
||||
<p>Aside: At this point we could use this Coalgebra to construct (unsorted) binary trees from lists:</p>
|
||||
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>example1 <span class="ot">=</span> ana unflatten [<span class="dv">1</span>,<span class="dv">3</span>] <span class="op">==</span> <span class="dt">Fix</span> (<span class="dt">NodeF</span> (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">1</span>)) (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">3</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>example1 <span class="ot">=</span> ana unflatten [<span class="dv">1</span>,<span class="dv">3</span>] <span class="op">==</span> <span class="dt">Fix</span> (<span class="dt">NodeF</span> (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">1</span>)) (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">3</span>)))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>On to our sorting, tree-collapsing Algebra. Which again is just a creepy word for “function that is used to deconstruct datastructures”.</p>
|
||||
<p>The function <code>mergeList</code> is defined below and just merges two sorted lists into one sorted list in O(n), I would probably take this from the <code>ordlist</code> package if I were to implement this <em>for real</em>.</p>
|
||||
<p>Again we see that we can just construct our sorted output list from a <code>TreeF</code> that apparently contains just lists.</p>
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">flatten ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> <span class="dt">TreeF</span> a [a] <span class="ot">-></span> [a]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">flatten ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> <span class="dt">TreeF</span> a [a] <span class="ot">-></span> [a]</span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>flatten <span class="dt">EmptyF</span> <span class="ot">=</span> []</span>
|
||||
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>flatten (<span class="dt">LeafF</span> c) <span class="ot">=</span> [c]</span>
|
||||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>flatten (<span class="dt">NodeF</span> l r) <span class="ot">=</span> mergeLists l r</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>flatten (<span class="dt">NodeF</span> l r) <span class="ot">=</span> mergeLists l r</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>Aside: We could use a Coalgebra to deconstruct trees:</p>
|
||||
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>example2 <span class="ot">=</span> cata flatten (<span class="dt">Fix</span> (<span class="dt">NodeF</span> (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">3</span>)) (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">1</span>)))) <span class="op">==</span> [<span class="dv">1</span>,<span class="dv">3</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>example2 <span class="ot">=</span> cata flatten (<span class="dt">Fix</span> (<span class="dt">NodeF</span> (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">3</span>)) (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">1</span>)))) <span class="op">==</span> [<span class="dv">1</span>,<span class="dv">3</span>]</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>Now we just combine the Coalgebra and the Algebra with one from the functions from Edwards <code>recursion-schemes</code> library:</p>
|
||||
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mergeSort ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mergeSort ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a]</span>
|
||||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>mergeSort <span class="ot">=</span> hylo flatten unflatten</span>
|
||||
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>example3 <span class="ot">=</span> mergeSort [<span class="dv">5</span>,<span class="dv">2</span>,<span class="dv">7</span>,<span class="dv">9</span>,<span class="dv">1</span>,<span class="dv">4</span>] <span class="op">==</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">7</span>,<span class="dv">9</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>example3 <span class="ot">=</span> mergeSort [<span class="dv">5</span>,<span class="dv">2</span>,<span class="dv">7</span>,<span class="dv">9</span>,<span class="dv">1</span>,<span class="dv">4</span>] <span class="op">==</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">7</span>,<span class="dv">9</span>]</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>What have we gained?</p>
|
||||
<p>We have implemented a MergeSort variant in 9 lines of code, not counting the <code>mergeLists</code> function below. Not bad, but <a href="http://en.literateprograms.org/Merge_sort_(Haskell)">this implementation</a> is not much longer.</p>
|
||||
@ -726,7 +731,7 @@ Note
|
||||
<hr>
|
||||
<p>At this point I wasn’t sure if this was useful or remotely applicable. Telling someone “I spend a whole weekend learning about Hylomorphism” isn’t something the cool developer kids do.</p>
|
||||
<p>It appeared to me that maybe I should have a look at the Core to see what the compiler finally comes up with (edited for brevity):</p>
|
||||
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ot"> mergeSort ::</span> [<span class="dt">Integer</span>] <span class="ot">-></span> [<span class="dt">Integer</span>]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ot"> mergeSort ::</span> [<span class="dt">Integer</span>] <span class="ot">-></span> [<span class="dt">Integer</span>]</span>
|
||||
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> mergeSort <span class="ot">=</span></span>
|
||||
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> \ (<span class="ot">x ::</span> [<span class="dt">Integer</span>]) <span class="ot">-></span></span>
|
||||
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> x <span class="kw">of</span> wild {</span>
|
||||
@ -753,17 +758,17 @@ Note
|
||||
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> p <span class="kw">of</span> _ { (ds1, y) <span class="ot">-></span> mergeSort y }))</span>
|
||||
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a> }</span>
|
||||
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a> }</span>
|
||||
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a> end <span class="dt">Rec</span> }</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a> end <span class="dt">Rec</span> }</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>While I am not really competent in reading Core and this is actually the first time I bothered to try, it is immediately obvious that there is no trace of any intermediate tree structure.</p>
|
||||
<p>This is when it struck me. I was dazzled and amazed. And am still. Although we are writing our algorithm as if we are working on a real tree structure the library and the compiler are able to just remove the whole intermediate step.</p>
|
||||
<hr>
|
||||
<p>Aftermath:</p>
|
||||
<p>In the beginning I promised a way to work on non-functor data structures. Actually that was how I began to work with the <code>recursion-schemes</code> library.</p>
|
||||
<p>We are able to create a ‘normal’ version of our tree from above:</p>
|
||||
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Tree</span> c <span class="ot">=</span> <span class="dt">Empty</span> <span class="op">|</span> <span class="dt">Leaf</span> c <span class="op">|</span> <span class="dt">Node</span> (<span class="dt">Tree</span> c) (<span class="dt">Tree</span> c)</span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Tree</span> c <span class="ot">=</span> <span class="dt">Empty</span> <span class="op">|</span> <span class="dt">Leaf</span> c <span class="op">|</span> <span class="dt">Node</span> (<span class="dt">Tree</span> c) (<span class="dt">Tree</span> c)</span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>But we can not use this directly with our (Co-)Algebras. Luckily Edward build a little bit of type magic into the library:</p>
|
||||
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Base</span> (<span class="dt">Tree</span> c) <span class="ot">=</span> (<span class="dt">TreeF</span> c)</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Base</span> (<span class="dt">Tree</span> c) <span class="ot">=</span> (<span class="dt">TreeF</span> c)</span>
|
||||
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Unfoldable</span> (<span class="dt">Tree</span> c) <span class="kw">where</span></span>
|
||||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> embed <span class="dt">EmptyF</span> <span class="ot">=</span> <span class="dt">Empty</span></span>
|
||||
@ -773,10 +778,10 @@ Note
|
||||
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Foldable</span> (<span class="dt">Tree</span> c) <span class="kw">where</span></span>
|
||||
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> project <span class="dt">Empty</span> <span class="ot">=</span> <span class="dt">EmptyF</span></span>
|
||||
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a> project (<span class="dt">Leaf</span> c) <span class="ot">=</span> <span class="dt">LeafF</span> c</span>
|
||||
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> project (<span class="dt">Node</span> l r) <span class="ot">=</span> <span class="dt">NodeF</span> l r</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> project (<span class="dt">Node</span> l r) <span class="ot">=</span> <span class="dt">NodeF</span> l r</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Without going into detail by doing this we establish a relationship between <code>Tree</code> and <code>TreeF</code> and teach the compiler how to translate between these types.</p>
|
||||
<p>Now we can use our Alebra on our non functor type:</p>
|
||||
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>example4 <span class="ot">=</span> cata flatten (<span class="dt">Node</span> (<span class="dt">Leaf</span> <span class="ch">'l'</span>) (<span class="dt">Leaf</span> <span class="ch">'r'</span>)) <span class="op">==</span> <span class="st">"lr"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>example4 <span class="ot">=</span> cata flatten (<span class="dt">Node</span> (<span class="dt">Leaf</span> <span class="ch">'l'</span>) (<span class="dt">Leaf</span> <span class="ch">'r'</span>)) <span class="op">==</span> <span class="st">"lr"</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>The great thing about this is that, looking at the Core output again, there is no traces of the <code>TreeF</code> structure to be found. As far as I can tell, the algorithm is working directly on our <code>Tree</code> type.</p>
|
||||
<hr>
|
||||
<p>Literature:</p>
|
||||
@ -788,13 +793,13 @@ Note
|
||||
</ul>
|
||||
<hr>
|
||||
<p>Appendix:</p>
|
||||
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mergeLists ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a] <span class="ot">-></span> [a]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mergeLists ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a] <span class="ot">-></span> [a]</span>
|
||||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>mergeLists <span class="ot">=</span> <span class="fu">curry</span> <span class="op">$</span> unfoldr c <span class="kw">where</span></span>
|
||||
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> c ([], []) <span class="ot">=</span> <span class="dt">Nothing</span></span>
|
||||
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> c ([], y<span class="op">:</span>ys) <span class="ot">=</span> <span class="dt">Just</span> (y, ([], ys))</span>
|
||||
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a> c (x<span class="op">:</span>xs, []) <span class="ot">=</span> <span class="dt">Just</span> (x, (xs, []))</span>
|
||||
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a> c (x<span class="op">:</span>xs, y<span class="op">:</span>ys) <span class="op">|</span> x <span class="op"><=</span> y <span class="ot">=</span> <span class="dt">Just</span> (x, (xs, y<span class="op">:</span>ys))</span>
|
||||
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> x <span class="op">></span> y <span class="ot">=</span> <span class="dt">Just</span> (y, (x<span class="op">:</span>xs, ys))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> x <span class="op">></span> y <span class="ot">=</span> <span class="dt">Just</span> (y, (x<span class="op">:</span>xs, ys))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
|
||||
|
||||
|
||||
@ -813,7 +818,7 @@ Note
|
||||
a.appendChild(i);
|
||||
window.document.body.appendChild(a);
|
||||
}
|
||||
window.setColorSchemeToggle(window.hasAlternateSentinel())
|
||||
setColorSchemeToggle(hasAlternateSentinel())
|
||||
const icon = "";
|
||||
const anchorJS = new window.AnchorJS();
|
||||
anchorJS.options = {
|
||||
|
Reference in New Issue
Block a user