summaryrefslogtreecommitdiff
path: root/examples/nqueens/nqueens.sin
blob: 47795862d4dfa1a76ec08229db89bc0ee0d992d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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]]]