module CurryBasics where import Control.Search.SetFunctions zs, thisIsOK :: [Int] zs = 0 : zs thisIsOK = take 3 zs -- gives finite list in finite time luckyNumber :: Int luckyNumber = 7 luckyNumber = 8 offByOne :: Int -> Int offByOne n = n + 1 offByOne n = n - 1 restrictEven :: Int -> Int restrictEven n | mod n 2 == 0 = n -- No answer if n is odd. -- Insert x anywhere in ys -- E.g., insert x [a,b] = [x,a,b] or [a,x,b] or [a,b,x]. insert x ys = x : ys insert x (y:ys) = y : insert x ys -- That helps with permutation. perm :: [a] -> [a] perm [] = [] perm (x:xs) = insert x (perm xs) -- case-of deterministic, first-match only prepend x xs = case xs of ys -> x : ys y:ys -> y : insert x ys -- this is dead code signExclusive :: (Num a, Ord a) => a -> String signExclusive x | x >= 0 = "non-neg" | x <= 0 = "non-pos" -- meaning: else-if -- signExclusive 0 has just one answer "non-neg" sign :: (Num a, Ord a) => a -> String sign x | x >= 0 = "non-neg" sign x | x <= 0 = "non-pos" -- meaning: two independent if's -- sign 0 has two answers, "non-neg" and "non-pos" unificationExample :: (Int, Int, [Int]) unificationExample | x : x : y : [] =:= 1 : y : ys = (x, y, ys) where x, y, ys free unificationExample0 :: Int unificationExample0 | x : [] =:= [] = x where x free occursCheckExample :: Bool occursCheckExample = xs =:= 0 : xs where xs free unificationExample2 :: (Int, Int, [Int]) unificationExample2 | x : x : y : [] =:= luckyNumber : y : ys = (x, y, ys) where x, y, ys free data N = Z | S N deriving (Ord, Eq, Show) two :: N two = S (S Z) add :: N -> N -> N add Z n = n add (S m) n = S (add m n) decomp3 :: N -> (N, N, N) decomp3 n | n =:= add (add x y) z = (x, y, z) where x = S x0 y = S y0 z = S z0 x0, y0, z0 free decomp3Exists :: N -> Bool decomp3Exists n = notEmpty (set1 decomp3 n) -- Faster than `decomp3Count n > 0` because notEmpty terminates as soon as -- an answer is found. decomp3Count :: N -> Int decomp3Count n = foldValues (+) 0 (mapValues (const 1) (set1 decomp3 n)) -- Faster than `length (decomp3s n)` because no sorting done. decomp3s :: N -> [(N, N, N)] decomp3s n = sortValues (set1 decomp3 n)