Опубликован: 19.09.2008 | Доступ: свободный | Студентов: 658 / 70 | Оценка: 4.50 / 5.00 | Длительность: 21:25:00
Тема: Программирование
Специальности: Программист, Архитектор программного обеспечения
Теги:
Лекция 9:
Стандартное начало (Prelude)
subtract :: (Num a) => a -> a -> a subtract = flip (-) even, odd :: (Integral a) => a -> Bool even n = n `rem` 2 == 0 odd = not . even gcd :: (Integral a) => a -> a -> a gcd 0 0 = error "Prelude.gcd: gcd 0 0 не определен" gcd x y = gcd' (abs x) (abs y) where gcd' x 0 = x gcd' x y = gcd' y (x `rem` y) lcm :: (Integral a) => a -> a -> a lcm _ 0 = 0 lcm 0 _ = 0 lcm x y = abs ((x `quot` (gcd x y)) * y) (^) :: (Num a, Integral b) => a -> b -> a x ^ 0 = 1 x ^ n | n > 0 = f x (n-1) x where f _ 0 y = y f x n y = g x n where g x n | even n = g (x*x) (n `quot` 2) | otherwise = f x (n-1) (x*y) _ ^ _ = error "Prelude.^: отрицательный показатель степени" (^^) :: (Fractional a, Integral b) => a -> b -> a x ^^ n = if n >= 0 then x^n else recip (x^(-n)) fromIntegral :: (Integral a, Num b) => a -> b fromIntegral = fromInteger . toInteger realToFrac :: (Real a, Fractional b) => a -> b realToFrac = fromRational . toRational
- Монадические классы
class Functor f where fmap :: (a -> b) -> f a -> f b class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a
- Минимальное полное определение:
- (>>=), return m >> k = m >>= \_ -> k fail s = error s sequence :: Monad m => [m a] -> m [a] sequence = foldr mcons (return []) where mcons p q = p >>= \x -> q >>= \y -> return (x:y) sequence_ :: Monad m => [m a] -> m () sequence_ = foldr (>>) (return ())
- Функции вида xxxM работают со списком аргументов, но повышают тип функции или - элемента списка до монадического типа
mapM :: Monad m => (a -> m b) -> [a] -> m [b] mapM f as = sequence (map f as) mapM_ :: Monad m => (a -> m b) -> [a] -> m () mapM_ f as = sequence_ (map f as) (=<<) :: Monad m => (a -> m b) -> m a -> m b f =<< x = x >>= f
- Тривиальный тип
data () = () deriving (Eq, Ord, Enum, Bounded)
- Недопустимо в Haskell; только для примера
- - Функциональный тип
- - идентичная функция
id :: a -> a id x = x
const :: a -> b -> a const x _ = x
(.) :: (b -> c) -> (a -> b) -> a -> c f . g = \ x -> f (g x)
- flip f принимает свои (первые) два аргумента в обратном порядке для f.
flip :: (a -> b -> c) -> b -> a -> c flip f x y = f y x seq :: a -> b -> b seq = ...
- Примитив - правоассоциативное инфиксное применение операторов - (полезно в стиле с возобновляющейся передачей)
($), ($!) :: (a -> b) -> a -> b f $ x = f x f $! x = x `seq` f x
data Bool = False | True deriving (Eq, Ord, Enum, Read, Show, Bounded)
- Булевы функции
(&&), (||) :: Bool -> Bool -> Bool True && x = x False && _ = False True || _ = True False || x = x not :: Bool -> Bool not True = False not False = True otherwise :: Bool otherwise = True
data Char = ... 'a' | 'b' ... - значения Unicode instance Eq Char where c == c' = fromEnum c == fromEnum c' instance Ord Char where c <= c' = fromEnum c <= fromEnum c' instance Enum Char where toEnum = primIntToChar fromEnum = primCharToInt enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Char)] enumFromThen c c' = map toEnum [fromEnum c, fromEnum c' .. fromEnum lastChar] where lastChar :: Char lastChar | c' < c = minBound | otherwise = maxBound instance Bounded Char where minBound = '\0' maxBound = primUnicodeMaxChar type String = [Char]
- Тип "может быть" (Maybe)
data Maybe a = Nothing | Just a deriving (Eq, Ord, Read, Show) maybe :: b -> (a -> b) -> Maybe a -> b maybe n f Nothing = n maybe n f (Just x) = f x instance Functor Maybe where fmap f Nothing = Nothing fmap f (Just x) = Just (f x) instance Monad Maybe where (Just x) >>= k = k x Nothing >>= k = Nothing return = Just fail s = Nothing
- Тип "или" (Either)
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show) either :: (a -> c) -> (b -> c) -> Either a b -> c either f g (Left x) = f x either f g (Right y) = g y
- Тип ввода - вывода
data IO a = ... - абстрактный instance Functor IO where fmap f x = x >>= (return . f) instance Monad IO where (>>=) = ... return = ... fail s = ioError (userError s)
- Тип упорядочивания
data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Read, Show, Bounded)