Skip to content

Commit

Permalink
add examples/code_golf directory
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-moen committed Jul 9, 2020
1 parent b78bfde commit fd1799a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/code_golf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code Golf in Curv
=================
"Code Golf" is a form of recreational programming where you try to write
the shortest program that accomplishes a given task.
29 changes: 29 additions & 0 deletions examples/code_golf/euler1.curv
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
1..999 >> filter(n->n `mod` [3,5] `equal` 0 >> or) >> sum

// Project Euler #1 (https://projecteuler.net/problem=1)
//
// If we list all the natural numbers below 10 that are multiples of 3 or 5,
// we get 3, 5, 6 and 9. The sum of these multiples is 23.
//
// Find the sum of all the multiples of 3 or 5 below 1000.
// Answer is: 233168
//
// An imperative solution:
// do local total = 0;
// for (n in 1..999)
// if ((n `mod` 3) == 0 || (n `mod` 5) == 0)
// total := total + n;
// in total
//
// This can be transformed into a 2-element pipeline using a list comprehension:
// [for (n in 1..999) if ((n `mod` 3) == 0 || (n `mod` 5) == 0) n] >> sum
//
// Break it down to a 3-element pipeline using 'filter':
// 1..999 >> filter(n->(n `mod` 3) == 0 || (n `mod` 5) == 0) >> sum
//
// Note there is code duplication: the expression pattern n `mod` _ == 0
// appears twice. What if the input was a list of prime factors, instead of
// hard coding the two prime factors 3 and 5?
//
// Then we could write:
// 1..999 >> filter(n->n `mod` [3,5] `equal` 0 >> or) >> sum
5 changes: 5 additions & 0 deletions examples/code_golf/quine.curv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(s->"$s$(s)")"(s->$=$.s$.(s)$=)"

// A "Quine" is a program that prints itself.
// Since Curv is a pure functional language, I decided that a Quine in Curv
// is an expression returning a string of the program's source code.

0 comments on commit fd1799a

Please sign in to comment.