|
2 | 2 | <!doctype html>
|
3 | 3 | <html>
|
4 | 4 | <head>
|
5 |
| - <meta name="generator" content="Hugo 0.130.0"> |
| 5 | + <meta name="generator" content="Hugo 0.130.0"><script src="/livereload.js?mindelay=10&v=2&port=1313&path=livereload" data-no-instant defer></script> |
6 | 6 | <meta charset="UTF-8" />
|
7 | 7 | <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
8 | 8 | <link rel="stylesheet" href="/main.min.css" />
|
|
17 | 17 |
|
18 | 18 | <h1 id="cognate">Cognate</h1>
|
19 | 19 | <h2 id="readable-and-concise-concatenative-programming">Readable and concise concatenative programming</h2>
|
20 |
| -<div class="code"><pre><code><span style='font-style: italic;color: #969896'>~~ Fizzbuzz in Cognate</span> |
| 20 | +<!--cognate--><div class="code"><pre><code> |
| 21 | +~~ Fizzbuzz in Cognate |
21 | 22 |
|
22 |
| -<span style='color: #0086b3'>Def</span> <span style='color: #795da3'>Fizzbuzz</span> <span style='color: #4e4e4e'>(</span> |
23 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>N</span> <span style='color: #333333'>be</span> <span style='color: #183691'>Of</span> <span style='color: #4e4e4e'>(</span><span style='color: #183691'>Integer?</span><span style='color: #4e4e4e'>)</span>; |
24 |
| - <span style='color: #0086b3'>Def</span> <span style='color: #795da3'>Multiple</span> <span style='color: #333333'>as</span> <span style='color: #4e4e4e'>(</span><span style='color: #183691'>Zero?</span> <span style='color: #183691'>Modulo</span> <span style='color: #183691'>Swap</span> <span style='color: #795da3'>N</span><span style='color: #4e4e4e'>)</span>; |
25 |
| - <span style='color: #183691'>Print</span> |
26 |
| - <span style='color: #a71d5d'>If</span> <span style='color: #795da3'>Multiple</span> <span style='color: #333333'>of</span> <span style='color: #0086b3'>15</span> <span style='color: #333333'>then</span> <span style='color: #183691'>"fizzbuzz"</span> |
27 |
| - <span style='color: #a71d5d'>If</span> <span style='color: #795da3'>Multiple</span> <span style='color: #333333'>of</span> <span style='color: #0086b3'>3</span> <span style='color: #333333'>then</span> <span style='color: #183691'>"fizz"</span> |
28 |
| - <span style='color: #a71d5d'>If</span> <span style='color: #795da3'>Multiple</span> <span style='color: #333333'>of</span> <span style='color: #0086b3'>5</span> <span style='color: #333333'>then</span> <span style='color: #183691'>"buzz"</span> |
29 |
| - <span style='color: #333333'>else</span> <span style='color: #795da3'>N</span> |
30 |
| -<span style='color: #4e4e4e'>)</span>; |
| 23 | +Def Fizzbuzz ( |
| 24 | + Let N be Of (Integer?); |
| 25 | + Def Multiple as (Zero? Modulo Swap N); |
| 26 | + Print |
| 27 | + If Multiple of 15 then "fizzbuzz" |
| 28 | + If Multiple of 3 then "fizz" |
| 29 | + If Multiple of 5 then "buzz" |
| 30 | + else N |
| 31 | +); |
31 | 32 |
|
32 |
| -<span style='color: #a71d5d'>For</span> <span style='color: #333333'>each</span> <span style='color: #333333'>in</span> <span style='color: #183691'>Range</span> <span style='color: #0086b3'>1</span> <span style='color: #333333'>to</span> <span style='color: #0086b3'>100</span> <span style='color: #4e4e4e'>(</span><span style='color: #183691'>Print</span> <span style='color: #795da3'>Fizzbuzz</span><span style='color: #4e4e4e'>)</span> |
33 |
| -</code></pre></div><p>Cognate is a project aiming to create a human readable programming language with as little syntax as possible. Where natural language programming usually uses many complex syntax rules, instead Cognate takes them away. What it adds is simple, a way to embed comments into statements.</p> |
34 |
| -<div class="code"><pre><code><span style='font-style: italic;color: #969896'>~~ Towers of Hanoi in Cognate</span> |
| 33 | +For each in Range 1 to 100 (Print Fizzbuzz); |
| 34 | +</code></pre></div> |
| 35 | +<p>Cognate is a project aiming to create a human readable programming language with as little syntax as possible. Where natural language programming usually uses many complex syntax rules, instead Cognate takes them away. What it adds is simple, a way to embed comments into statements.</p> |
| 36 | +<!--cognate--><div class="code"><pre><code> |
| 37 | +~~ Towers of Hanoi in Cognate |
35 | 38 |
|
36 |
| -<span style='color: #0086b3'>Def</span> <span style='color: #795da3'>Move</span> <span style='color: #333333'>discs</span> <span style='color: #333333'>as</span> <span style='color: #4e4e4e'>(</span> |
| 39 | +Def Move discs as ( |
37 | 40 |
|
38 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>N</span> <span style='color: #333333'>be</span> <span style='color: #333333'>number</span> <span style='color: #333333'>of</span> <span style='color: #333333'>discs</span>; |
39 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>A</span> <span style='color: #333333'>be</span> <span style='color: #333333'>first</span> <span style='color: #333333'>rod</span>; |
40 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>B</span> <span style='color: #333333'>be</span> <span style='color: #333333'>second</span> <span style='color: #333333'>rod</span>; |
41 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>C</span> <span style='color: #333333'>be</span> <span style='color: #333333'>third</span> <span style='color: #333333'>rod</span>; |
| 41 | + Let N be number of discs; |
| 42 | + Let A be first rod; |
| 43 | + Let B be second rod; |
| 44 | + Let C be third rod; |
42 | 45 |
|
43 |
| - <span style='color: #a71d5d'>Unless</span> <span style='color: #183691'>Zero?</span> <span style='color: #795da3'>N</span> <span style='color: #4e4e4e'>(</span> |
44 |
| - <span style='color: #795da3'>Move</span> <span style='font-weight: bold;color: #4e4e4e'>-</span> <span style='color: #0086b3'>1</span> <span style='color: #795da3'>N</span> <span style='color: #333333'>discs</span> <span style='color: #333333'>from</span> <span style='color: #795da3'>A</span> <span style='color: #333333'>via</span> <span style='color: #795da3'>C</span> <span style='color: #333333'>to</span> <span style='color: #795da3'>B</span>; |
45 |
| - <span style='color: #183691'>Prints</span> <span style='color: #4e4e4e'>(</span><span style='color: #183691'>"Move disc "</span> <span style='color: #795da3'>N</span> <span style='color: #183691'>" from "</span> <span style='color: #795da3'>A</span> <span style='color: #183691'>" to "</span> <span style='color: #795da3'>C</span><span style='color: #4e4e4e'>)</span>; |
46 |
| - <span style='color: #795da3'>Move</span> <span style='font-weight: bold;color: #4e4e4e'>-</span> <span style='color: #0086b3'>1</span> <span style='color: #795da3'>N</span> <span style='color: #333333'>discs</span> <span style='color: #333333'>from</span> <span style='color: #795da3'>B</span> <span style='color: #333333'>via</span> <span style='color: #795da3'>A</span> <span style='color: #333333'>to</span> <span style='color: #795da3'>C</span>; |
47 |
| - <span style='color: #4e4e4e'>)</span> |
48 |
| -<span style='color: #4e4e4e'>)</span>; |
| 46 | + Unless Zero? N ( |
| 47 | + Move - 1 N discs from A via C to B; |
| 48 | + Prints ("Move disc " N " from " A " to " C); |
| 49 | + Move - 1 N discs from B via A to C; |
| 50 | + ) |
| 51 | +); |
49 | 52 |
|
50 |
| -<span style='color: #795da3'>Move</span> <span style='color: #0086b3'>5</span> <span style='color: #333333'>discs</span> <span style='color: #333333'>from</span> <span style='color: #183691'>"a"</span> <span style='color: #333333'>via</span> <span style='color: #183691'>"b"</span> <span style='color: #333333'>to</span> <span style='color: #183691'>"c"</span> |
51 |
| -</code></pre></div><p>As you can see, Cognate ignores words starting with lowercase letters, allowing them to be used to describe functionality and enhance readability. This makes Cognate codebases intuitive and maintainable.</p> |
52 |
| -<div class="code"><pre><code><span style='font-style: italic;color: #969896'>~~ Square numbers in Cognate</span> |
| 53 | +Move 5 discs from "a" via "b" to "c"; |
| 54 | +</code></pre></div> |
| 55 | +<p>As you can see, Cognate ignores words starting with lowercase letters, allowing them to be used to describe functionality and enhance readability. This makes Cognate codebases intuitive and maintainable.</p> |
| 56 | +<!--cognate--><div class="code"><pre><code> |
| 57 | +~~ Square numbers in Cognate |
53 | 58 |
|
54 |
| -<span style='color: #0086b3'>Def</span> <span style='color: #795da3'>Square</span> <span style='color: #333333'>as</span> <span style='color: #4e4e4e'>(</span><span style='font-weight: bold;color: #4e4e4e'>*</span> <span style='color: #183691'>Twin</span><span style='color: #4e4e4e'>)</span>; |
55 |
| -<span style='color: #183691'>Map</span> <span style='color: #4e4e4e'>(</span><span style='color: #795da3'>Square</span><span style='color: #4e4e4e'>)</span> <span style='color: #333333'>over</span> <span style='color: #183691'>Range</span> <span style='color: #0086b3'>1</span> <span style='color: #333333'>to</span> <span style='color: #0086b3'>10</span>; |
56 |
| -<span style='color: #183691'>Print</span> |
57 |
| -</code></pre></div><p>Cognate is a stack-oriented programming language similar to Forth or Factor, except statements are evaluated right to left. This gives the expressiveness of concatenative programming as well as the readability of prefix notation. Statements can be delimited at arbitrary points, allowing them to read as sentences would in English.</p> |
58 |
| -<div class="code"><pre><code><span style='font-style: italic;color: #969896'>~~ Prime numbers in Cognate</span> |
| 59 | +Def Square as (* Twin); |
| 60 | +Map (Square) over Range 1 to 10; |
| 61 | +Print; |
| 62 | +</code></pre></div> |
| 63 | +<p>Cognate is a stack-oriented programming language similar to Forth or Factor, except statements are evaluated right to left. This gives the expressiveness of concatenative programming as well as the readability of prefix notation. Statements can be delimited at arbitrary points, allowing them to read as sentences would in English.</p> |
| 64 | +<!--cognate--><div class="code"><pre><code> |
| 65 | +~~ Prime numbers in Cognate |
59 | 66 |
|
60 |
| -<span style='color: #0086b3'>Def</span> <span style='color: #795da3'>Factor</span> <span style='color: #4e4e4e'>(</span><span style='color: #183691'>Zero?</span> <span style='color: #183691'>Modulo</span> <span style='color: #183691'>Swap</span><span style='color: #4e4e4e'>)</span>; |
| 67 | +Def Factor (Zero? Modulo Swap); |
61 | 68 |
|
62 |
| -<span style='color: #0086b3'>Def</span> <span style='color: #795da3'>Primes</span> <span style='color: #4e4e4e'>(</span> |
63 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>U</span> <span style='color: #333333'>is</span> <span style='color: #333333'>upper</span> <span style='color: #333333'>bound</span>; |
64 |
| - <span style='color: #333333'>initially</span> <span style='color: #794da3'>List</span> <span style='color: #4e4e4e'>(</span><span style='color: #4e4e4e'>)</span>; |
65 |
| - <span style='color: #a71d5d'>For</span> <span style='color: #183691'>Range</span> <span style='color: #0086b3'>2</span> <span style='color: #333333'>to</span> <span style='color: #795da3'>U</span> <span style='color: #4e4e4e'>(</span> |
66 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>P</span> <span style='color: #333333'>is</span> <span style='color: #333333'>potential</span> <span style='color: #333333'>prime</span>; |
67 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>Found</span> <span style='color: #333333'>be</span> <span style='color: #333333'>list</span> <span style='color: #333333'>of</span> <span style='color: #333333'>found</span> <span style='color: #333333'>primes</span>; |
68 |
| - <span style='color: #0086b3'>Let</span> <span style='color: #795da3'>To-check</span> <span style='color: #333333'>be</span> <span style='color: #a71d5d'>Take-while</span> <span style='color: #4e4e4e'>(</span><span style='font-weight: bold;color: #4e4e4e'><=</span> <span style='color: #183691'>Sqrt</span> <span style='color: #795da3'>P</span><span style='color: #4e4e4e'>)</span> <span style='color: #795da3'>Found</span>; |
69 |
| - <span style='color: #a71d5d'>When</span> <span style='color: #183691'>All</span> <span style='color: #4e4e4e'>(</span><span style='font-weight: bold;color: #4e4e4e'>Not</span> <span style='color: #795da3'>Factor</span> <span style='color: #333333'>of</span> <span style='color: #795da3'>P</span><span style='color: #4e4e4e'>)</span> <span style='color: #795da3'>To-check</span> <span style='color: #4e4e4e'>(</span> |
70 |
| - <span style='color: #183691'>Append</span> <span style='color: #795da3'>P</span> |
71 |
| - <span style='color: #4e4e4e'>)</span> <span style='color: #333333'>to</span> <span style='color: #795da3'>Found</span> |
72 |
| - <span style='color: #4e4e4e'>)</span> |
73 |
| -<span style='color: #4e4e4e'>)</span>; |
| 69 | +Def Primes ( |
| 70 | + Fold ( |
| 71 | + Let I be our potential prime; |
| 72 | + Let Primes are the found primes; |
| 73 | + Let To-check be Take-while (<= Sqrt I) Primes; |
| 74 | + When None (Factor of I) To-check |
| 75 | + (Append List (I)) to Primes; |
| 76 | + ) from List () over Range from 2 |
| 77 | +); |
74 | 78 |
|
75 |
| -<span style='color: #183691'>Print</span> <span style='color: #795da3'>Primes</span> <span style='color: #333333'>up</span> <span style='color: #333333'>to</span> <span style='color: #0086b3'>1000</span>; |
76 |
| -</code></pre></div><p>Cognate borrows from other concatenative languages, but also adds unique features of its own.</p> |
| 79 | +Print Primes up to 1000; |
| 80 | +</code></pre></div> |
| 81 | +<p>Cognate borrows from other concatenative languages, but also adds unique features of its own.</p> |
77 | 82 | <ul>
|
78 | 83 | <li>Point-free functions</li>
|
79 | 84 | <li>Operation chaining</li>
|
|
0 commit comments