From 00ca368c2c6864fd059e3ad655afd279b5e75748 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Wed, 8 Aug 2018 19:04:01 -0700 Subject: Add exercise from this morning --- fpco.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100755 fpco.hs diff --git a/fpco.hs b/fpco.hs new file mode 100755 index 0000000..22ff391 --- /dev/null +++ b/fpco.hs @@ -0,0 +1,23 @@ +#! /usr/bin/env nix-shell +#! nix-shell -p haskellPackages.ghc -i runghc + +{-# OPTIONS_GHC -Wall #-} + +{- Problem: write a function that removes the largest value in the list. + - + - My first solution used 'Data.List.maximum' and 'Data.List.delete', which + - solved the problem but was also inefficient because it would consume the + - entire list at most twice. After talking over the problem, Snoyman showed me + - a more efficient solution that, due to Haskell's lazy eval, only holds one + - element of the list at a time: + - -} + +deleteLargestValue :: [Integer] -> [Integer] +deleteLargestValue [] = [] +deleteLargestValue (x:xs) = loop x xs + where loop _ [] = [] + loop a (b:cs) = if a > b then b:(loop a cs) else a:(loop b cs) + +main :: IO () +main = do + putStrLn $ "Expect [1,2]: " ++ (show $ deleteLargestValue [1, 2, 3]) -- cgit v1.2.3