Skip to content

Instantly share code, notes, and snippets.

@deniok
Created March 23, 2013 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deniok/5228821 to your computer and use it in GitHub Desktop.
Save deniok/5228821 to your computer and use it in GitHub Desktop.
ФП АУ код к лекции 5
module Fp05 where
-- переставляет элементы пары местами (определена в Data.Tuple)
swap :: (a,b) -> (b,a)
swap (x,y) = (y,x)
-- перечисление
data Color = Red | Green | Blue | Indigo | Violet deriving Show
isRGB :: Color -> Bool
isRGB Red = True
isRGB Green = True
isRGB Blue = True
isRGB _ = False -- Wild-card
-- декартово произведение
data PointDouble = PtD Double Double deriving Show
midPointDouble :: PointDouble -> PointDouble -> PointDouble
midPointDouble (PtD x1 y1) (PtD x2 y2) = PtD ((x1 + x2) / 2) ((y1 + y2) / 2)
-- полиморфные типы
data Point a = Pt a a deriving Show
midPoint :: Fractional a => Point a -> Point a -> Point a
midPoint (Pt x1 y1) (Pt x2 y2) = Pt ((x1 + x2) / 2) ((y1 + y2) / 2)
-- рекурсивные типы
data List a = Nil | Cons a (List a) deriving Show
len :: List a -> Int
len (Cons _ xs) = 1 + len xs
len Nil = 0
-- выражение case ... of ...
head' xs = case xs of
(x:_) -> x
[] -> error "head': empty list"
-- ленивые образцы
(***) f g ~(x, y) = (f x, g y)
-- As-шаблон
dupFirst :: [a] -> [a]
dupFirst (x:xs) = x:x:xs
dupFirst' :: [a] -> [a]
dupFirst' s@(x:xs) = x:s
-- Метки полей (Field Labels)
data Point' a = Pt' {ptx, pty :: a} deriving Show
absP p = sqrt (ptx p ^ 2 + pty p ^ 2)
absP' (Pt' {ptx = x, pty = y}) = sqrt (x ^ 2 + y ^ 2)
-- Стандартные алгебраические типы
head'' :: [a] -> Either String a
head'' (x:_) = Right x
head'' [] = Left "head'': empty list"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment