Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Haskell Online

--{-# OPTIONS_GHC -Wall #-}
--module HW05 where
module Main where

data AbstractInteger = Zero
                     | Succ AbstractInteger
                     | Pred AbstractInteger
                     deriving (Show, Eq)

-- Задача 1 -----------------------------------------
instance Ord AbstractInteger where
--   (<=) = undefined
    compare (Pred _) (Pred _) = EQ
    compare (Pred _) _ = LT
    compare (Succ _) (Succ _) = EQ
    compare (Succ _) _ = GT
    compare Zero (Succ _) = LT
    compare Zero (Pred _) = GT
    compare Zero Zero = EQ

-- Задача 2 ----------------------------------------
aiToInteger :: AbstractInteger -> Integer
--aiToInteger = undefined
aiToInteger Zero = 0
aiToInteger (Pred ai)
  | ai == Zero = -1
  | otherwise = aiToInteger ai - 1
aiToInteger (Succ ai)
  | ai == Zero = 1
  | otherwise = aiToInteger ai + 1

-- Задача 3 -----------------------------------------
plusAbs :: AbstractInteger -> AbstractInteger -> AbstractInteger
--plusAbs = undefined
plusAbs ai1 ai2 = plusAbs' ai1 ai2
  where
    plusAbs' Zero a = a
    plusAbs' (Pred a1') (Succ a2') = plusAbs a1' a2'
    plusAbs' a1@(Pred _) (Pred a2') = plusAbs (Pred a1) a2'
    plusAbs' a1@(Succ _) (Succ a2') = plusAbs (Succ a1) a2'
    plusAbs' a1 a2 = plusAbs a2 a1

-- Задача 4 -----------------------------------------
timesAbs :: AbstractInteger -> AbstractInteger -> AbstractInteger
--timesAbs = undefined
timesAbs ai1 ai2 = ai2
{-
timesAbs  (Pred (Pred Zero)) (Pred (Pred (Pred Zero))) =  
                =    Succ( Succ ( Succ (Succ (Succ (Succ Zero)))))
-}

-- Задача 5 -----------------------------------------
instance Num AbstractInteger  where
    (+)   = plusAbs
    (*)   = timesAbs
    negate      = undefined
    fromInteger = undefined
    abs         = undefined
    signum      = undefined

-- Задача 6 -----------------------------------------
factorial :: (Eq a, Num a) => a -> a
factorial = undefined

-- Задача  7 -----------------------------------------
data Quaternion = Quaternion Double Double Double Double deriving (Eq)

instance Show Quaternion where
    show = undefined

-- Задача 8 -----------------------------------------
plusQuaternion :: Quaternion -> Quaternion -> Quaternion
plusQuaternion = undefined

-- Задача 9 -----------------------------------------
timesQuaternion :: Quaternion -> Quaternion -> Quaternion
timesQuaternion = undefined

--- Задача 10 ----------------------------------------
instance Num Quaternion  where
    (+)   = plusQuaternion
    (*)   = timesQuaternion
    negate      = undefined
    fromInteger = undefined
    abs         = undefined
    signum      = undefined
    
main :: IO ()
main = do
print $ "1. " ++ show (compare Zero (Succ Zero))
print $ "1. " ++ show (compare (Pred Zero) (Succ Zero))
print $ "1. " ++ show (Zero <= (Succ Zero))
print $ "1. " ++ show ((Pred Zero) == (Succ Zero))
print $ "2. " ++ show (aiToInteger Zero)
print $ "2. " ++ show (aiToInteger (Pred (Pred Zero)))
print $ "2. " ++ show (aiToInteger (Pred (Pred (Pred Zero))))
print $ "2. " ++ show (aiToInteger (Succ (Pred (Pred (Pred (Pred Zero))))))
print $ "3. " ++ show (plusAbs (Pred (Pred Zero))  (Succ (Succ Zero)))
print $ "3. " ++ show (plusAbs (Pred Zero)  (Succ (Succ Zero)))
print $ "4. " ++ show (timesAbs  (Pred (Pred Zero)) (Pred (Pred (Pred Zero))))

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.