From 3624c712d72d246f21d4e710cec7c11e052e0326 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 9 Dec 2025 16:32:32 +0100 Subject: Add the haskell book --- Haskell-book/09/src/PoemLines.purs | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Haskell-book/09/src/PoemLines.purs (limited to 'Haskell-book/09/src/PoemLines.purs') diff --git a/Haskell-book/09/src/PoemLines.purs b/Haskell-book/09/src/PoemLines.purs new file mode 100644 index 0000000..05a9a86 --- /dev/null +++ b/Haskell-book/09/src/PoemLines.purs @@ -0,0 +1,42 @@ +module PoemLines where + +import Data.Array ((:)) +import Data.String (takeWhile, dropWhile, drop) +import Prelude + +splitWith :: Char -> String -> Array String +splitWith _ "" = [] +splitWith sep s = (takeWhile cond s) : (splitWith sep $ drop 1 $ dropWhile cond s) + where cond x = x /= sep + +myWords :: String -> Array String +myWords = splitWith ' ' + +firstSen :: String +firstSen = "Tyger Tyger, burning bright\n" +secondSen :: String +secondSen = "In the forests of the night\n" +thirdSen :: String +thirdSen = "What immortal hand or eye\n" +fourthSen :: String +fourthSen = "Could frame thy fearful symmetry?" + +sentences :: String +sentences = firstSen <> secondSen <> thirdSen <> fourthSen +-- putStrLn sentences -- should print +-- Tyger Tyger, burning bright +-- In the forests of the night +-- What immortal hand or eye +-- Could frame thy fearful symmetry? + +myLines :: String -> Array String +myLines = splitWith '\n' + +got :: Array String +got = myLines sentences + +shouldEqual :: Array String +shouldEqual = [ "Tyger Tyger, burning bright" + , "In the forests of the night" + , "What immortal hand or eye" + , "Could frame thy fearful symmetry?" ] -- cgit v1.2.3