Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solved the first problem using common lisp.
- Loading branch information
Showing
3 changed files
with
33 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,23 @@ | ||
(in-package :nisp.euler) | ||
|
||
(defun sum-integers-if (predicate start end &optional (step 1)) | ||
"Sum numbers satisfying PREDICATE from START to END." | ||
(iterate (for n :from start :to end :by step) | ||
(when (funcall predicate n) | ||
(sum n :into result)) | ||
(declare (fixnum result)) ; Help the compiler optimize. | ||
(finally (return result)))) | ||
|
||
(defun solution-1/1 () | ||
"Call `sum-integers-if' after we compose predicate. | ||
This is a purely functional way of solving this problem." | ||
(sum-integers-if (disjoin (compose (curry #'= 0) (rcurry #'mod 3)) | ||
(compose (curry #'= 0) (rcurry #'mod 5))) | ||
0 999)) | ||
|
||
(defun solution-1/2 () | ||
"Use a local function definition for the predicate." | ||
(flet ((validp (n) | ||
(or (= 0 (mod n 3)) (= 0 (mod n 5))))) | ||
(sum-integers-if #'validp 0 999))) |
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,7 @@ | ||
(asdf:defsystem :nisp.euler | ||
:serial t | ||
:components | ||
((:file "package") | ||
(:module "1" | ||
:components | ||
((:file "sum-integers"))))) |
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,3 @@ | ||
(defpackage #:nisp.euler | ||
(:use :cl :iterate :alexandria) | ||
(:nicknames :euler)) |