diff options
| author | Tom Harley | 2019-12-23 01:32:39 +0000 |
|---|---|---|
| committer | Tom Harley | 2019-12-23 01:32:39 +0000 |
| commit | 7bc67b95161a34f8eca635d4000d9602508a9243 (patch) | |
| tree | 585b3778dc1967832390e5829e4b3909b4184a2c /examples/nqueens | |
| parent | Add basics for interpreter; add CLI options (diff) | |
| download | sinter-7bc67b95161a34f8eca635d4000d9602508a9243.tar.gz sinter-7bc67b95161a34f8eca635d4000d9602508a9243.zip | |
checkpoint
Diffstat (limited to 'examples/nqueens')
| -rw-r--r-- | examples/nqueens/list.sin | 56 | ||||
| -rw-r--r-- | examples/nqueens/nqueens.sin | 67 |
2 files changed, 123 insertions, 0 deletions
diff --git a/examples/nqueens/list.sin b/examples/nqueens/list.sin new file mode 100644 index 0000000..9a59945 --- /dev/null +++ b/examples/nqueens/list.sin @@ -0,0 +1,56 @@ +[type cons_ [kind head tail]] +[type nil_ [kind]] + +[def cons [head tail] + [cons_ 1 head tail]] + +[def nil [] + [nil_ 0]] + +[def head [xs] + [cons_.head xs]] + +[def tail [xs] + [cons_.tail xs]] + +[def has_tail [xs] + [nil_.kind xs]] + +[def is_nil [x] + [if [nil_.kind x] + 0 + 1]] + +[def concat [as bs] + [cons [head as] + [if [is_nil [tail as]] + bs + [concat [tail as] bs]]]] + +[def alt_concat [as bs] + [if [is_nil [tail as]] + [cons [head as] bs] + [cons [head as] [concat [tail as] bs]]]] + +[def flatten_once [xs] + [concat [head xs] [flatten_once [tail xs]]]] + +[def List.>>= [ma f] + [flatten_once [map ma f]]] + +[def List.return [a] + [cons a [nil]]] + +[def prepend [x xs] + [cons x xs]] + +[def append [xs x] + [if [is_nil xs] + [cons [debug_int x] [nil]] + [cons [head xs] [append [tail xs] x]]]] + +[def map [xs f] + [if [is_nil xs] + xs + [cons [f [head xs]] + [map [tail xs] f]]]] diff --git a/examples/nqueens/nqueens.sin b/examples/nqueens/nqueens.sin new file mode 100644 index 0000000..4779586 --- /dev/null +++ b/examples/nqueens/nqueens.sin @@ -0,0 +1,67 @@ +[dec cons [head tail]] +[dec nil []] +[dec head [xs]] +[dec tail [xs]] +[dec length [xs]] + +[type pair [(INT left) (INT right)]] + +[def nsoln [nq] + [length [gen nq nq]]] + +[def inter_main [] + [debug_int [nsoln 4]]] + +[def (INT ==) [(INT a) (INT b)] + [if [- a b] 1 0]] + +[def (INT not) [(INT x)] + [if x 0 1]] + +[def (INT !=) [(INT a) (INT b)] + [not [== a b]]] + +[def (INT and) [(INT a) (INT b)] + [if a [if b 1 0] 0]] + +[def (INT length) [(INT xs)] + [debug_int [if xs [+ 1 [length [cons.tail xs]]] 0]]] + +[def (INT count) [(INT from) (INT to)] + [if [== from to] to [cons from [count [+ from 1] to]]]] + +[def (INT concat) [(INT as) (INT bs)] + [if [cons.tail as] + [cons [cons.head as] [concat [cons.tail as] bs]] + [cons [cons.head as] bs]]] + +[def (INT combo2) [(INT a) (INT bs)] + [if bs + [cons [pair a [cons.head bs]] [combo2 a [cons.tail bs]]] + 0]] + +[def (INT combo) [(INT as) (INT bs)] + [if as + [concat [combo2 [cons.head as] bs] [combo [cons.tail as] bs]] + 0]] + +[def (INT safe) [(INT x) (INT d) (INT qs)] + [if qs + [and [and [!= x [cons.head qs]] + [!= x [+ [cons.head qs] d]]] + [and [!= x [- [cons.head qs] d]] + [safe x [+ d 1] [cons.tail qs]]]] + 1]] + +[def (INT safe_filter) [(INT xs)] + [if [safe [pair.left xs] 1 [pair.right xs]] + [cons [cons.head xs] [safe_filter [cons.tail xs]]] + [safe_filter [cons.tail xs]]]] + +[def (INT gen) [(INT n) (INT nq)] + [if n [safe_filter [combo [gen [- n 1] nq] [count 1 nq]]] 0]] + +[def (INT nsoln) [(INT nq)] + [length [gen nq nq]]] + +[def (INT inter_main) [] [debug_int [nsoln 4]]] |
