Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,31 @@
Object subclass:#Amb
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category:'Rosetta'
!
Smalltalk::Notification subclass:#FoundSolution
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
privateIn:Amb
!
!Amb::FoundSolution methods!
defaultAction
^ parameter
! !
!Amb class methods!
try:values in:aBlock
values do:[:each |
|rslt|
(rslt := aBlock value:each) notNil ifTrue:[
"hint: Notifications simply return nil, if there is no handler"
(FoundSolution raiseRequestWith:rslt) notNil ifTrue:[ ^ rslt ].
].
].
^ nil
! !

View file

@ -0,0 +1,11 @@
result :=
Amb try:#('the' 'that' 'a') in:[:w1 |
Amb try:#('frog' 'elephant' 'thing') in:[:w2 |
w2 first = w1 last ifTrue:[
Amb try:#('walked' 'traded' 'grows') in:[:w3 |
w3 first = w2 last ifTrue:[
Amb try:#('slowly' 'quickly') in:[:w4 |
Transcript showCR: e'trying {{w1 . w2 . w3 . w4}}'. "debug trace only"
w4 first = w3 last ifTrue:[
{w1 . w2 . w3 . w4} ]]]]]]].
Transcript showCR: e'found solution: {result}'

View file

@ -0,0 +1,9 @@
result :=
Amb try:(1 to:11) in:[:x |
Amb try:(1 to:11) in:[:y |
Amb try:(1 to:11) in:[:z |
(x squared + y squared = z squared) ifTrue:[
{x . y . z}
].
]]].
Transcript showCR: e'found rectangle {result}'.

View file

@ -0,0 +1,21 @@
!Amb class methods!
until:questionBlock in:aBlock
"compute solutions, asking if more solutions are to be searched
via questionBlock (gets the found solution as arg)"
|allResults|
allResults := OrderedCollection new.
aBlock on:FoundSolution do:[:ex |
allResults add:ex parameter.
(questionBlock value:ex parameter) ifTrue:[^ allResults].
ex proceedWith:nil.
].
^ allResults
!
allSolutions:aBlock
^ self until:[:solution | false] in:aBlock
!

View file

@ -0,0 +1,9 @@
result :=
Amb allSolutions:[
Amb try:(1 to:11) in:[:x |
Amb try:(1 to:11) in:[:y |
y <= x ifTrue:[
Amb try:(1 to:11) in:[:z |
(x squared + y squared = z squared) ifTrue:[
{x . y . z} ]]]]]].
Transcript showCR: e'all rectangles: {result}'.

View file

@ -0,0 +1,12 @@
result :=
Amb
until:[:solution |
(Dialog confirm: e'Found solution: {solution}\nSee more?') not
] in:[
Amb try:(1 to:100) in:[:x |
Amb try:(1 to:100) in:[:y |
y <= x ifTrue:[
Amb try:(1 to:100) in:[:z |
(x squared + y squared = z squared) ifTrue:[
{x . y . z} ]]]]]].
Transcript showCR: result.

View file

@ -0,0 +1,15 @@
tryAll:valuesCollection in:aBlock
"try each set of values in aBlock;
aBlock's number of args must be the number of elements in valuesCollection"
|tryAll|
tryAll :=
[:valuesCollection :argsIn |
valuesCollection isEmpty ifTrue:[
aBlock valueWithArguments:argsIn
] ifFalse:[
self try:(valuesCollection first) in:[:arg |
tryAll value:(valuesCollection from:2) value:argsIn,{arg} ]]].
^ tryAll value:valuesCollection value:#()

View file

@ -0,0 +1,12 @@
Amb
tryAll:#(
('the' 'that' 'a')
('frog' 'elephant' 'thing')
('walked' 'traded' 'grows')
('slowly' 'quickly')
) in:[:w1 :w2 :w3 :w4 |
(w2 first = w1 last
and:[ w3 first = w2 last
and:[ w4 first = w3 last
]]) ifTrue:[
{w1 . w2 . w3 . w4} ]]

View file

@ -0,0 +1,29 @@
"/ find { s. e. n. d. m. o. r. y.}
"/ such that:
"/ send
"/ + more
"/ -------
"/ = money
result :=
Amb try:(0 to:9) in:[:s |
Amb try:(0 to:9) in:[:e | e~=s ifTrue:[
Amb try:(0 to:9) in:[:n | (n~=e)&(n~=s) ifTrue:[
Amb try:(0 to:9) in:[:d | (d~=n)&(d~=e)&(d~=s) ifTrue:[
Amb try:(1 to:1) in:[:m | (m~=d)&(m~=n)&(m~=e)&(m~=s) ifTrue:[
Amb try:(0 to:9) in:[:o | (o~=m)&(o~=d)&(o~=n)&(o~=e)&(o~=s) ifTrue:[
Amb try:(0 to:9) in:[:r | (r~=o)&(r~=m)&(r~=d)&(r~=n)&(r~=e)&(r~=s) ifTrue:[
Amb try:(0 to:9) in:[:y | (y~=r)&(y~=o)&(y~=m)&(y~=d)&(y~=n)&(y~=e)&(y~=s) ifTrue:[
(
( (1000 * s) + (100 * e) + (10 * n) + d)
+ ( (1000 * m) + (100 * o) + (10 * r) + e)
= ((10000 * m) + (1000 * o) + (100 * n) + (10 * e) + y)
) ifTrue:[
Transcript showCR: e' {s}{e}{n}{d}'.
Transcript showCR: e' + {m}{o}{r}{e}'.
Transcript showCR: e' -----------'.
Transcript showCR: e' = {m}{o}{n}{e}{y}'.
{'s'->s . 'e'->e . 'n'->n . 'd'->d . 'm'->m . 'o'->o . 'r'->r . 'y'->y }
].
]]]]]]]]]]]]]]].
Transcript cr; showCR: result.