しりとり

やっぱり OCaml も使えないといかんなーと思う今日このごろ。文字列の扱いが最も戸惑う。文字のリストでいいじゃんという感じ。そして Haskell には、Char できちんとユニコードが扱えることを望みます。

let rec delete e = function
    [] -> []
  | x::xs when x=e -> xs
  | x::xs -> x :: delete e xs
let rec shiritori' v r =
  if v = []
  then List.rev r
  else amb v (fun x ->
                let w = List.hd r in
                  if w.[String.length w - 1] = x.[0]
                  then shiritori' (delete x v) (x::r)
                  else ambfail ())
let shiritori = function
    [] -> []
  | v  ->
      try amb v (fun x -> shiritori' (delete x v) [x])
      with AMBFAIL -> []