forked from curv3d/curv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |