From e677c12cddb3dff20fbe237e929cca77c5b13dc5 Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 5 Apr 2021 03:36:32 +0100 Subject: [PATCH] Demonstrate returning values from functions --- Unit 1/Lesson 4/returningFunctions.hs | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Unit 1/Lesson 4/returningFunctions.hs diff --git a/Unit 1/Lesson 4/returningFunctions.hs b/Unit 1/Lesson 4/returningFunctions.hs new file mode 100644 index 0000000..a485dfb --- /dev/null +++ b/Unit 1/Lesson 4/returningFunctions.hs @@ -0,0 +1,29 @@ +sfOffice :: ([Char], [Char]) -> [Char] +sfOffice name = if surname < "L" + then nameText ++ " - PO Box 1234 - San Francisco, CA, 94111" + else nameText ++ " - PO Box 1010 - San Francisco, CA, 94109" + where surname = snd name + nameText = fst name ++ " " ++ surname + + +nyOffice :: ([Char], [Char]) -> [Char] +nyOffice name = nameText ++ ": PO Box 789 - New York, NY, 10013" + where nameText = fst name ++ " " ++ snd name + + +renoOffice :: (a, [Char]) -> [Char] +renoOffice name = nameText ++ " - PO Box 456 - Reno, NV 89523" + where nameText = snd name + + +getLocation :: [Char] -> ([Char], [Char]) -> [Char] +getLocation location = case location of + "ny" -> nyOffice + "sf" -> sfOffice + "reno" -> renoOffice + _ -> (\name -> fst name ++ " " ++ snd name) + + +addressLetter :: ([Char], [Char]) -> [Char] -> [Char] +addressLetter name location = locationFunction name + where locationFunction = getLocation location