Rhywun (26) [Avatar] Offline
#1
I have everything working but I'm trying to figure out how to add newlines like the sample output. The best I can do is the exact output I want but all on one line. I tried various combinations of \n, putStrLn, and do without luck.

Here is my best attempt, without the newlines:

patientSummary :: Patient -> String
patientSummary p =
    "**************" ++
    "Patient Name: " ++ showName (name p) ++
    "Sex: " ++ showSex (sex p) ++
    "Age: " ++ show (age p) ++
    "Height: " ++ show (height p) ++
    "Weight: " ++ show (weight p) ++
    "Blood Type: " ++ showBloodType (bloodType p) ++
    "**************"
    where 
        showSex Male = "Male"
        showSex Female = "Female"


How do I print a string with a newline in it??
icogs (14) [Avatar] Offline
#2

You can use the '\n' character to embed newlines into you strings. To display them correctly you can use the putStr function.

e.g. in GHCi
Prelude> putStr "Line 1\nLine 2\n"
Line 1
Line 2
Prelude>

Rhywun (26) [Avatar] Offline
#3
OK, I got it, with a lot of carefully-placed parentheses... I gave up trying to return a String....

-- patientSummary :: Patient -> String
patientSummary p =
    do
        putStrLn "**************"
        putStrLn ("Patient Name: " ++ (showName (name p)))
        putStrLn ("Sex: " ++ (showSex (sex p)))
        putStrLn ("Age: " ++ (show (age p)))
        putStrLn ("Height: " ++ (show (height p)))
        putStrLn ("Weight: " ++ (show (weight p)))
        putStrLn ("Blood Type: " ++ (showBloodType (bloodType p)))
        putStrLn "**************"
    where 
        showSex Male = "Male"
        showSex Female = "Female"