summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xfpco.hs23
1 files changed, 23 insertions, 0 deletions
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])