module Apps.Sudoku.Extra;

import Anoma.Prelude open;

import Data.Set as Set;
open Set using {Set};

ordNub {A : Type} {{Ord A}} : List A -> List A :=
  let
    go : Set A -> List A -> List A
      | _ nil := nil
      | s (x :: xs) :=
        if (Set.member? x s) (go s xs) (x :: go (Set.insert x s) xs);
  in go Set.empty;

hasDuplicates {A : Type} {{Ord A}} (xs : List A) : Bool :=
  length (ordNub xs) /= length xs;

allEq {A : Type} {{Eq A}} : List A -> Bool
  | nil := true
  | (x :: xs) :=
    let
      go : List A -> Bool
        | nil := true
        | (y :: ys) := if (x == y) (go ys) false;
    in go xs;

terminating
chunksOf {A : Type} : Nat -> List A -> List (List A)
  | _ nil := nil
  | n xs := case splitAt n xs of {ys, zs := ys :: chunksOf n zs};

--- Insert the first ;String; at the beginning, in between, and at the end of
--- the second list
surround (x : String) (xs : List String) : List String :=
  (x :: intersperse x xs) ++ [x];

--- Insert the first ;String; in between the ;String;s in the second list and
--- concatenates the result
intercalate (sep : String) (xs : List String) : String :=
  concatStr (intersperse sep xs);
Last modified on 2023-12-07 10:36 UTC