-- tasks -- 1 ---------------------------- data Date = Date Int Int Int deriving Show today = Date 2020 10 14 -- 1.1 -- Úloha 1.1 -- Definujte funkciu isLastInMonth a pomocou nej funkciu nextDay: isLeapYear :: Int -> Bool isLeapYear year | year `mod` 400 == 0 = True | year `mod` 100 == 0 = False | year `mod` 4 == 0 = True | otherwise = False isLastInMonth :: Date -> Bool isLastInMonth (Date y m d) = d == daysInMonth y m daysInMonth :: Int -> Int -> Int daysInMonth y m | m == 2 = if isLeapYear y then 29 else 28 | m `elem` [4,6,9,11] = 30 | otherwise = 31 nextDay :: Date -> Date nextDay (Date y m d) | not (isLastInMonth (Date y m d)) = Date y m (d + 1) | m == 12 = Date (y + 1) 1 1 | otherwise = Date y (m + 1) 1 -- Algebraické dátové typy cieľ -- 2 -- ------------------------------------------------------------- -- Haskell podporuje algebraické dátové typy, takže dátový typ môže mať niekoľko konštruktorov s rôznymi typmi parametrov. -- Poznámka -- Konštruktor nie je to isté, ako názov typu, no oba sa môžu volať rovnako, hlavne ak je konštruktor iba jeden. -- Definujme vlastný typ pre tvary, konkrétne obdĺžnik a kruh: data Point = Point Double Double deriving (Eq, Show) data Shape = Circle Point Double | Rectangle Point Point deriving Show smallCircle = Circle (Point 0 0) 1 largeCircle = Circle (Point 0 0) 10 someRectangle = Rectangle (Point 0 0) (Point 10 5) aSquare = Rectangle (Point 0 0) (Point 10 10) shapes = [smallCircle, largeCircle, someRectangle, aSquare] -- Konštuktory typu sa dajú použiť aj pri porovnávaní vzorov na ľavej strane definície funkcie: area :: Shape -> Double area (Circle _ r) = pi * r^2 area (Rectangle (Point x1 y1) (Point x2 y2)) = (abs (x2-x1)) * (abs (y2-y1)) -- Úloha 2.1 -- Definujte funkciu perimeter, ktorá vypočíta obvod zadaného útvaru: perimeter :: Shape -> Double perimeter -- Úloha 2.2 -- Definujte funkciu move, ktorá posunie útvar o zadaný vektor: data Vector = Vec Double Double deriving (Eq, Show) move :: Vector -> Shape -> Shape