Kalábovi

Kalábovic wikina

Uživatelské nástroje

Nástroje pro tento web


flp:start

Toto je starší verze dokumentu!


Funkcionální a logické programování

Haskell? Yeah, I use it to program our starship.

Lambda kalkul

Lambda calculus, Church encoding, http://safalra.com/science/lambda-calculus

  • α-konverze – λx.xyα λz.zy, substituce1)
  • β-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.xFT λx.xz.F)T λp.pFr.T) λx.xFT
AND λxy.xyF λxy.yz.x)F λab.abr.F) λxy.xyF
OR λxy.xTy λxy.yz.T)x λab.aTr.b) λxy.xTy
XOR λxy.x(NOT y)y λxy.xz.(NOT y))y λxy.x(NOT y)(λz.y) λxy.x(NOT y)y
EQ λxy.xy(NOT y) λxy.xz.y)(NOT y) λxy.xyz.(NOT y)) λxy.xy(NOT y)
<Presci> 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)
<Presci> 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.mv.FALSE)TRUE
if then else λctf.ctf
tuple λfse.efs
first λp.pab.a)2)
second λp.pab.b)3)

Haskell

--Sepsal Mike T
import IO
 
{-
-- Jen pro pripomenuti:
data IOMode =  ReadMode | WriteMode | AppendMode | ReadWriteMode
getLine :: IO String
putStrLn :: String -> IO ()
type FilePath = [Char] -- tj. jméno souboru
openFile :: FilePath -> IOMode -> IO Handle
hIsEOF :: Handle -> IO Bool
hGetLine :: Handle -> IO String
hClose :: Handle -> IO ()
hGetContents :: Handle -> IO String
readFile :: FilePath -> IO String
lines :: String -> [String]
unlines :: [String] -> String
words :: String -> [String]
unwords :: [String] -> String
-}
 
-- Spocte pocet radku v souboru.
countLines file = do
  content <- readFile file
  putStrLn $ show $ length $ lines content
 
-- Spocte pocet slov v prvnich n radcich v souboru.
countWordsN file n = do
    content <- readFile file
    putStrLn $ show $ length $ words $ unlines $ take n $ lines content
 
-- Prokladane vypise obsah souboru na vystup.
prokladane file1 file2 = do
     h1 <- openFile file1 ReadMode
     h2 <- openFile file2 ReadMode
     c1 <- hGetContents h1
     c2 <- hGetContents h2
     write (lines c1) (lines c2)
     hClose h1
     hClose h2
   where
    write [] _ = return ()
    write _ [] = return ()
    write (x:xs) (y:ys) = do
      putStrLn x
      putStrLn y
      write xs ys
 
-- Vypise obsah souboru s cisly radky.
printWithLineNumber file = do
    h <- openFile file ReadMode
    c <- hGetContents h
    write (lines c) 1
    hClose h
  where
    write [] _ = return ()
    write (x:xs) n = do
      putStrLn $ (show n) ++ ". " ++ x
      write xs (n+1)
 
-- Vypise radky na vystup, ktere jsou v obou souborech, ve stejnem poradi.
copyOut file1 file2 = do
    h1 <- openFile file1 ReadMode
    h2 <- openFile file2 ReadMode
    c1 <- hGetContents h1
    c2 <- hGetContents h2
    putStr $ unlines $ [x | x <- lines c1, y <- lines c2, x == y]
    hClose h1
    hClose h2
 
-- Nacte radek a slova vypise v opacnem poradi.
reverseOut = do
  line <- getLine
  if null line
    then return ()
    else do
      putStrLn $ rev line
      reverseOut
  where rev l = unwords $ foldl (\acc x -> x : acc) []  (words l)

Strukturální indukce

Structural induction, 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

Prolog

1)
Bacha na volné a vázané proměnné!
2)
TRUE
3)
FALSE
/var/www/wiki/data/attic/flp/start.1337949234.txt.gz · Poslední úprava: (upraveno mimo DokuWiki)