====== Funkcionální a logické programování ====== {{ https://www.verifyrecruitment.com/blog/wp-content/uploads/2014/08/haskell-spock-jpg.jpg?300|Haskell? Yeah, I use it to program our starship.}} ===== Lambda kalkul ===== [[wp>Lambda calculus]], [[wp>Church encoding]] * **α-konverze** -- λ//x//.//xy// →α λ//z//.//zy//, substituce((Bacha na volné a vázané proměnné!)) * **β-konverze** -- (λ//xz//.//xz//)(//xy//) →β λ//z//.//xyz//, aplikace funkce * **η-konverze** -- λ//x//.(//uv//)//x// →η //uv// ^ ''T'' ^ λ//xy//.//x// ^ λ//xy//.//xy// ^ λ//xy//.//x// ^ λ//xy//.//x// ^ ^ ''F'' ^ λ//xy//.//y// ^ λ//xy//.//y// ^ λ//xy//.//yx// ^ λ//xy//.//xy// ^ ^ ''NOT'' | λ//x//.//x''FT''// | λ//x//.//x//(λ//z//.//''F''//)//''T''// | λ//p//.//p''F''//(λ//r//.//''T''//) | λ//x//.//x''FT''// | ^ ''AND'' | λ//xy//.//xy''F''// | λ//xy//.//y//(λ//z//.//x//)//''F''// | λ//ab//.//ab//(λ//r//.//''F''//) | λ//xy//.//xy''F''// | ^ ''OR'' | λ//xy//.//x''T''y// | λ//xy//.//y//(λ//z//.//''T''//)//x// | λ//ab//.//a''T''//(λ//r//.//b//) | λ//xy//.//x''T''y// | ^ ''XOR'' | λ//xy//.//x//(//''NOT'' y//)//y// | λ//xy//.//x//(λ//z//.(//''NOT'' y//))//y// | λ//xy//.//x//(//''NOT'' y//)(λ//z//.//y//) | λ//xy//.//x//(//''NOT'' y//)//y// | ^ ''EQ'' | λ//xy//.//xy//(//''NOT'' y//) | λ//xy//.//x//(λ//z//.//y//)(//''NOT'' y//) | λ//xy//.//xy//(λ//z//.(//''NOT'' y//)) | λ//xy//.//xy//(//''NOT'' y//) | Pitel: no ne vsechny, ale kdyz ti true nebo false budou vracet dve hodnodty (LET TRUE = \a b.b a, tak tu jednu musis nejak zabit (treba v notu) a zabijes ji tam, ze ji nahradis \a.False treba, takze "a" se zahodi a zbyte False ^ 0, 1, 2, 3, ... |λ//fx//.//x//\\ λ//fx//.//fx//\\ λ//fx//.//f//(//fx//)\\ λ//fx//.//f//(//f//(//fx//))\\ λ//fx//.//fⁿx//| ^ succ | λ//nfx//.//f//(//nfx//) | ^ add | λ//mnfx//.//mf//(//nfx//) | ^ mult | λ//mnf//.//m//(//nf//) | ^ //mⁿ// | λ//mn//.//nm// | ^ iszero | λ//m//.//m//(λ//v//.//''FALSE''//)//''TRUE''// | ^ if then else | λ//ctf//.//ctf// | ^ tuple | λ//fse//.//efs// | ^ first | λ//p//.//p//(λ//ab//.//a//)((''TRUE'')) | ^ second | λ//p//.//p//(λ//ab//.//b//)((''FALSE'')) | ===== Haskell ===== * [[http://learnyouahaskell.com|Learn You a Haskell for Great Good!]] * **[[https://gist.github.com/2634678|Gist]]** ===== Strukturální indukce ===== [[wp>Structural induction]], [[http://www.haskell.org/onlinereport/standard-prelude.html|Standard Prelude]] foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) foldl :: (a -> b -> a) -> a -> [b] -> a foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs (++) :: [a] -> [a] -> [a] [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys) take :: Int -> [a] -> [a] take n _ | n <= 0 = [] take _ [] = [] take n (x:xs) = x : take (n-1) xs drop :: Int -> [a] -> [a] drop n xs | n <= 0 = xs drop _ [] = [] drop n (_:xs) = drop (n-1) xs head :: [a] -> a head (x:_) = x head [] = error "Prelude.head: empty list" tail :: [a] -> [a] tail (_:xs) = xs tail [] = error "Prelude.tail: empty list" last :: [a] -> a last [x] = x last (_:xs) = last xs last [] = error "Prelude.last: empty list" init :: [a] -> [a] init [x] = [] init (x:xs) = x : init xs init [] = error "Prelude.init: empty list" length :: [a] -> Int length [] = 0 length (_:l) = 1 + length l filter :: (a -> Bool) -> [a] -> [a] -- filter p xs = [x | x <- xs, p x] filter p [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs takeWhile :: (a -> Bool) -> [a] -> [a] takeWhile p [] = [] takeWhile p (x:xs) | p x = x : takeWhile p xs | otherwise = [] dropWhile :: (a -> Bool) -> [a] -> [a] dropWhile p [] = [] dropWhile p xs@(x:xs') | p x = dropWhile p xs' | otherwise = xs zipWith :: (a->b->c) -> [a]->[b]->[c] zipWith z (a:as) (b:bs) = z a b : zipWith z as bs zipWith _ _ _ = [] ===== Prolog ===== [[99pl]]