Squashed commit of the following: commit 82aa61982e9c65f65f221fbbcdb41fe8a21321ec Author: AKP <abi@tdpain.net> Date: Thu Nov 9 12:33:56 2023 +0000 Question 5 commit 7239dcaa65545c9a5b8b634213c813438c8e52f8 Author: AKP <abi@tdpain.net> Date: Thu Nov 9 11:47:10 2023 +0000 Question 4 commit 507520373221469ea2f71ee44b67f51dbf9a2455 Author: AKP <abi@tdpain.net> Date: Thu Nov 9 11:40:56 2023 +0000 Question 3 commit 7138150a9c7f34ceb4fdb9c10a018b9cd45cb196 Author: AKP <abi@tdpain.net> Date: Thu Nov 9 11:11:26 2023 +0000 Question 2 commit 808293dff53bd033ea9138ef01a4cb2dfab8fbca Author: AKP <abi@tdpain.net> Date: Thu Nov 9 11:05:24 2023 +0000 Question 1 commit 9f4b80f7e887c0b3665e95ef1be1678e116ec2bc Author: AKP <abi@tdpain.net> Date: Thu Nov 9 11:00:25 2023 +0000 Add base files
59 lines
1.8 KiB
Haskell
59 lines
1.8 KiB
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 Test1 ( evenMajority
|
|
, get5SmoothNumbers
|
|
, comesBefore
|
|
, countApplications
|
|
, f
|
|
) where
|
|
|
|
import Types
|
|
|
|
{- QUESTION 1 -}
|
|
|
|
_countEvens :: [Int] -> Int
|
|
_countEvens ns = foldl (\acc x -> if even x then acc + 1 else acc) 0 ns
|
|
|
|
evenMajority :: [Int] -> Bool
|
|
evenMajority ns = let half = (length ns) `div` 2 in (_countEvens ns) > half
|
|
|
|
{- QUESTION 2 -}
|
|
|
|
_is5Smooth :: Int -> Bool
|
|
_is5Smooth n = foldl (\acc x -> acc && x <= 5) True (primeFactors n)
|
|
|
|
get5SmoothNumbers :: Int -> [Int]
|
|
get5SmoothNumbers n = filter _is5Smooth [1..n]
|
|
|
|
{- QUESTION 3 -}
|
|
|
|
_getStationsBefore :: TrainStop -> Maybe TrainStop -> [TrainStop]
|
|
_getStationsBefore finalStop Nothing = _getStationsBefore finalStop (Just Redditch)
|
|
_getStationsBefore finalStop (Just previousStop) | finalStop == previousStop = []
|
|
| otherwise = previousStop:(_getStationsBefore finalStop (Just (theStopAfter previousStop)))
|
|
|
|
comesBefore :: TrainStop -> TrainStop -> Bool
|
|
comesBefore s1 s2 = not (foldl (\acc x -> acc && x /= s1) True (_getStationsBefore s2 Nothing))
|
|
|
|
{- QUESTION 4 -}
|
|
|
|
_countApplications :: (a -> a) -> (a -> Bool) -> a -> Int -> Int
|
|
_countApplications f p acc tries | (p acc) = tries
|
|
| otherwise = _countApplications f p (f acc) (tries + 1)
|
|
|
|
countApplications :: (a -> a) -> (a -> Bool) -> a -> Int
|
|
countApplications f p x = _countApplications f p x 0
|
|
|
|
{- QUESTION 5 -}
|
|
|
|
_cloneArgument :: (a -> a -> r) -> (a -> r)
|
|
_cloneArgument f = (\x -> f x x)
|
|
|
|
f :: (a -> a -> r) -> ((a -> r) -> a) -> r
|
|
f g h = let i = (_cloneArgument g) in
|
|
g (h i) (h i)
|