Skip to content

Instantly share code, notes, and snippets.

View vituscze's full-sized avatar

Vít Šefl vituscze

View GitHub Profile
factors1 :: Integer -> Integer -> [Integer]
factors1 k n
| k * k > n = [n]
| n `mod` k == 0 = k : factors1 k (n `div` k)
| otherwise = factors1 (k + 1) n
factors :: Integer -> [Integer]
factors 1 = []
factors n = factors1 2 n
pos(P) :- member(P, [a,b,c,d]).
next(go(P), [M, Box1, Box2, B], [P, Box1, Box2, B]) :- pos(M), pos(P).
next(stack, [M, M, M, B], [M, box2, M, B]).
next(unstack, [M, box2, M, B], [M, M, M, B]).
next(push(box1, P), [M, M, Box2, B], [P, P, Box2, B]) :- pos(P).
next(push(box2, P), [M, Box1, M, B], [P, Box1, P, B]) :- pos(P).
next(climb_on_1, [M, Box1, Box2, B], [box1, Box1, Box2, B]) :- M = Box1; Box1 = box2, M = Box2.
next(climb_off_1, [box1, Box1, Box2, B], [M, Box1, Box2, B]) :- pos(Box1), M = Box1; Box1 = box2, M = Box2.
next(grab, [box1, box2, B, B], [box1, box2, B, hand]).
match_one(one(L), LIn, LOut) :- append(L, LOut, LIn).
match_one(star(L), LIn, LOut) :- LIn = LOut; match_one(plus(L), LIn, LOut).
match_one(plus(L), LIn, LOut) :- match_one(one(L), LIn, LMid), match_one(star(L), LMid, LOut).
match_one(opt(L), LIn, LOut) :- LIn = LOut; match_one(one(L), LIn, LOut).
match(Expr, Str) :- foldl(match_one, Expr, Str, []).
decode_char(Key, A, B) :- B #= (A - 97 + Key) mod 26 + 97.
decode_string(_, _, [], []).
cone([o,o,o], 0).
cone(R, 1) :- select(x, R, [o,o]).
cone(R, 2) :- select(o, R, [x,x]).
cone([x,x,x], 3).
match([_,_], []).
match([A,B,C|R], [M|RM]) :- cone([A,B,C],M), match([B,C|R], RM).
mines(Count, Mines) :- same_length(Count, Mines), append([[o],Mines,[o]], M), match(M, Count).
sublist(L, M) :-
M = [_|_],
append(_, Suffix, L),
append(M, _, Suffix).
subseq([], []).
subseq([X|L], [X|M]) :- subseq(L, M).
subseq([_|L], M) :- subseq(L, M).
disjoint([], [], []).
module Main where
import Control.Monad
import Data.List
import Data.Maybe
modifyAt :: Int -> (a -> a) -> [a] -> [a]
modifyAt pos f l = ll ++ case lr of
[] -> []
x:xs -> f x:xs
import Data.List
data Prop
= Const Bool
| Var Char
| Not Prop
| And Prop Prop
| Or Prop Prop
deriving (Show)
import Data.Function
class Queue q where
emptyQueue :: q a
isEmpty :: q a -> Bool
enqueue :: a -> q a -> q a
dequeue :: q a -> (a, q a)
data SQueue a = SQ [a] [a]
data Tree = Nil | Node Tree Int Tree deriving (Eq, Ord, Show)
allBalanced :: Int -> [Tree]
allBalanced = go 1
where
go from to
| from > to = [Nil]
| otherwise = [Node l x r | x <- [h .. h + m], l <- go from (x - 1), r <- go (x + 1) to]
where
(h, m) = (from + to) `divMod` 2
import Data.List
is_equiv :: (a -> a -> Bool) -> [a] -> Bool
is_equiv (~=) set = refl && sym && trans
where
infix 1 ==>
x ==> y = not x || y
every c = all c set