-
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
Oghenevwogaga Ebresafe
committed
Apr 19, 2021
1 parent
65226cd
commit 2ac8172
Showing
1 changed file
with
66 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,66 @@ | ||
### Solving cryptarithmetic problems in Racket | ||
|
||
This is just a simple library to solve simple cryptarithmetic problems. | ||
|
||
You just call a single function `solve`. | ||
|
||
For example to solve this: | ||
|
||
![SEND + MORE = MONEY](send.png) | ||
|
||
You'd just have to call the `solve` function like so: | ||
|
||
```racket | ||
(solve '(S E N D) | ||
'(M O R E) | ||
'(M O N E Y)) | ||
``` | ||
|
||
This returns a (lazy) stream of solutions: | ||
|
||
```racket | ||
#<stream> | ||
``` | ||
|
||
Most *good* crytarithmetic problems usually have a single solution. | ||
|
||
To get a solution just use the standard `racket/stream` functions: | ||
|
||
|
||
```racket | ||
(define s | ||
(solve '(S E N D) | ||
'(M O R E) | ||
'(M O N E Y))) | ||
(stream-first s) | ||
``` | ||
And sure enough, you'd get: | ||
|
||
```racket | ||
'((M . 1) | ||
(N . 6) | ||
(O . 0) | ||
(Y . 2) | ||
(D . 7) | ||
(E . 5) | ||
(R . 8) | ||
(S . 9)) | ||
``` | ||
If a particular crytparithmetic problem doesn't have a solution and you | ||
try to invoke `stream-first` on its stream you'd get an exception saying | ||
you it expected a non-empty stream: | ||
|
||
```racket | ||
stream-first: contract violation | ||
expected: (and/c stream? (not/c stream-empty?)) | ||
given: #<stream> | ||
``` | ||
|
||
Note that it might take a while to get a solution because it searches | ||
for a solution and returns the first it finds and stops seraching. | ||
|
||
if | ||
|
||
|
||
|