From f4e417571a0409c55f730a95692add114bec6dce Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 8 Apr 2025 15:35:05 +0200 Subject: [PATCH] added assignment 6 ex 3 --- src/Assignment6/Ex3.sc | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/Assignment6/Ex3.sc diff --git a/src/Assignment6/Ex3.sc b/src/Assignment6/Ex3.sc new file mode 100644 index 0000000..8bc35d2 --- /dev/null +++ b/src/Assignment6/Ex3.sc @@ -0,0 +1,22 @@ +val cities = List("Paris", "London", "Berlin", "Lausanne") +val relatives = List("Grandma", "Grandpa", "Aunt Lottie", "Dad") +val travellers = List("Pierre-Andre", "Rachel") + +def generatePostcards(cities: List[String], relatives: List[String], travellers: List[String]): List[String] = { + for (t <- travellers; + r <- relatives; + c <- cities) yield s"Dear $r, Wish you were here in $c! Love, $t" +} + +def generatePostcards2(cities: List[String], relatives: List[String], travellers: List[String]): List[String] = { + for (t <- travellers; + r <- relatives; + c <- cities; + if r.startsWith("G")) yield s"Dear $r, Wish you were here in $c! Love, $t" +} + +val cards: List[String] = generatePostcards(cities, relatives, travellers) +println(cards.mkString("\n")) + +val cards2: List[String] = generatePostcards2(cities, relatives, travellers) +println(cards2.mkString("\n"))