Сделать 5 функций на Haskell

4 000 руб. за проект
01 декабря 2022, 11:13 • 5 откликов • 28 просмотров
Написать 5 функций на haskell, которые работают с описанными в файле типами данных типа Customer, Product, order и так далее

Дедлайн: 2 декабря 23:59

Шаблоны функций имеются в приложенном файле Orders.hs, 1я даже практически реализована

numProducts
productQuantities
majority orders
shortfall
share orders deliveries

Конкретное описание функции, сигнатура и принцип работы - указаны в комментариях файла рядом с функцией

module Orders where

import Data.List (sort)
import Data.List (groupBy)
import Data.Function (on)

type Customer = String
type Product = String

-- An order of some positive quantity of a product by a customer
data Order = Order Customer Product Double
deriving (Show)

-- A delivery to the supplier of some quantity of a product
data Delivery = Delivery Product Double
deriving (Show)

-- Collect weight for 2 tuples
collectWeight :: (Customer, Integer) -> (Customer, Integer) -> (Customer, Integer)
collectWeight list1 list2 = (fst list1, snd list1 + snd list2)

-- All customers who submitted an order, with the number of different
-- products each of them ordered.
numProducts :: [Order] -> [(Customer, Integer)]
numProducts = map (foldl1 collectWeight) . groupBy ((==) `on` fst) . sort . map (\(Order c p q) -> (c, p))

-- All products that have been ordered, with the total quantity of each.
productQuantities :: [Order] -> [(Product, Double)]
productQuantities orders = map (foldl1 collectWeight) . groupBy ((==) `on` fst) . sort . map (\(Order c p q) -> (p, q))

-- The customers and products for which the customer has ordered
-- more than half the total quantity for that product.
majority :: [Order] -> [(Customer, Product)]
majority orders = undefined

-- Products for which the total quantity ordered exceeds the
-- total quantity delivered, with the difference in quantity.
shortfall :: [Order] -> [Delivery] -> [(Product, Double)]
shortfall orders deliveries = undefined

--Allocation of quantities of products to customers.
--
--No customer should be allocated more of a given product than they have
--ordered. If a sufficient quantity of a product has been delivered to
--satisfy all orders, each customer should receive the total quantity
--they ordered. If not, the available quantity of the product should
--be shared between customers in proportion to the amount ordered.

-- For example, if the delivered quantity of a product is half the the
-- total ordered quantity of that product, each customer would receive
-- half of what they ordered.
share :: [Order] -> [Delivery] -> [(Customer, Product, Double)]
share orders deliveries = undefined