This repository has been archived on 2025-07-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
fp/week3/homework.hs
2023-10-24 18:33:11 +01:00

29 lines
896 B
Haskell

-- setting the "warn-incomplete-patterns" flag asks GHC to warn you
-- about possible missing cases in pattern-matching definitions
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
-- see https://wiki.haskell.org/Safe_Haskell
{-# LANGUAGE Safe #-}
module Homework3 (gasUsage , luhnDouble , luhn) where
import Types
gasUsage :: (Fractional a, Ord a) => a -> Classification
gasUsage g | g < 3 = Low
| 3 <= g && g < 5 = Medium
| 5 <= g && g < 7 = High
| otherwise = SuperHigh
luhnDouble :: Int -> Int
luhnDouble x = let y = x * 2 in
if y > 9 then y - 9 else y
luhn :: Int -> Int -> Int -> Int -> Bool
luhn a b c d = let x = [a, b, c, d] in
sum (
map
(\(i, y) -> if i `mod` 2 == 0 then luhnDouble y else y)
(zip [1..4] (reverse x))
) `mod` 10 == 0