Jq语言
八皇后问题例子

回溯法求解八皇后问题，采用函数式编程风格的条件表达式和递归函数可以写为：
def queens:
    def place(r; c; pl):
        all(pl[]; .[0] != c
            and .[1] != r+c
            and .[2] != r-c);
    def q(r): . as $pl
        | if r < 8 then
            range(0; 8) | . as $c
            | select(place(r; $c; $pl))
            | $pl+[[., r+., r-.]]
            | q(r+1)
          else
            map(.[0]) end;
    [] | q(0)
    | map("abcdefgh"[.:.+1]) | to_entries
    | map(.value+(.key+1 | tostring)) | sort;
queens
将这段代码保存入queens.jq文件中，下面演示其执行结果并提取其92个解中的前两个解：
$ jq -nc -f ./queens.jq | wc -l
92
$ jq -nc -f ./queens.jq | sed -n '1,2p'
["a1","b7","c5","d8","e2","f4","g6","h3"]
["a1","b7","c4","d6","e8","f2","g5","h3"]
在能采用可逆（reversible）赋值和静态变量的语言比如Icon的实现中，其3个位置列表共有8+15+15 = 38个元素，而这个存储传递风格实现对3个位置列表进行了zip（英语：）变换，共有3*9*(0+8)/2 = 108个元素。
进一步采用隐式编程风格，上述代码可以写为：
def queens:
    def q(r): . as $pl
        | range(0; 8)
        | def place: . as $c
            | all($pl[]; .[0] != $c
                and .[1] != r+$c
                and .[2] != r-$c); 
          select(place)
        | $pl+[[., r+.,r-.]];
    def pipeline(i; n): 
        q(i) | if i+1 < n then pipeline(i+1; n) end;
    [] | pipeline(0; 8) | map(.[0])
    | map("abcdefgh"[.:.+1]) | to_entries
    | map(.value+(.key+1 | tostring)) | sort;
queens
它还可以采用共递归运算recurse()写为：
def queens:
    def q: . as $pl
        | $pl[1] as $r | select($r < 8)
        | range(0; 8)
        | def place: . as $c
            | all($pl[0][]; .[0] != $c
                and .[1] != $r+$c
                and .[2] != $r-$c); 
          select(place)
        | [$pl[0]+[[., $r+., $r-.]], $r+1]; 
    [[], 0] | recurse(q) 
    | select(.[1] == 8) | .[0] | map(.[0])
    | map("abcdefgh"[.:.+1]) | to_entries
    | map(.value+(.key+1 | tostring)) | sort;
queens
最后，通过旋转和反射将可行解归并为独立解：
def queens:
    def q: . as $pl 
        | $pl[1] as $r
        | range(0; 8)
        | def place: . as $c
            | all($pl[0][]; .[0] != $c
                and .[1] != $r+$c
                and .[2] != $r-$c); 
          select(place)
        | [$pl[0]+[[., $r+., $r-.]], $r+1];
    def toletter: 
        "abcdefghijklmnopqrstuvwxyz"[.:.+1];
    def fund_solut(f):
        def inverse:
            to_entries | map([.value, .key]) 
            | sort | map(.[1]);
        def variants:
            [., inverse] | map(., reverse)
            | map(., map(7-.)) 
            | map(map(toletter) | add);
        foreach f as $i 
            ([null, {}]; .[1] as $ml
            | ($i | variants) as $nl
            | if all($nl[]; in($ml) | not) then
                [$i, ($ml | .[$nl[]]=null)]
              else
                [null, $ml] end;
            .[0])
        | select (. != null);
    def pipeline(n): 
        q | if n > 1 then pipeline(n-1) end;
    fund_solut([[], 0] | pipeline(8) | .[0] | map(.[0])) 
    | map(toletter) | to_entries
    | map(.value+(.key+1 | tostring)) | sort;
queens
下面演示其执行结果：
$ jq -nc -f ./queens.jq | wc -l
12
$ jq -nc -f ./queens.jq 
["a1","b7","c5","d8","e2","f4","g6","h3"]
["a1","b7","c4","d6","e8","f2","g5","h3"]
["a6","b1","c5","d2","e8","f3","g7","h4"]
["a4","b1","c5","d8","e2","f7","g3","h6"]
["a5","b1","c8","d4","e2","f7","g3","h6"]
["a3","b1","c7","d5","e8","f2","g4","h6"]
["a5","b1","c4","d6","e8","f2","g7","h3"]
["a7","b1","c3","d8","e6","f4","g2","h5"]
["a5","b1","c8","d6","e3","f7","g2","h4"]
["a5","b3","c1","d7","e2","f8","g6","h4"]
["a5","b7","c1","d4","e2","f8","g6","h3"]
["a6","b3","c1","d8","e4","f2","g7","h5"]