Haskell Line Feed - Updated

I have the following functions in Haskell that should print sales of weeks. Each sale in a new line. But it does not work as I expect. The problem is I have a newline character '\ n'.

the code:

printWeeks :: Int->String
printWeeks 0 = printWeek 0
printWeeks x  = printWeeks(x-1) ++ printWeek x 

printWeek :: Int->String
printWeek x = show(x)  ++  "  " ++ stars (sales x) ++ "'\n'"

I tried many ways, but the new line symbol does not work as expected. Everything is printed on the same line as not what I want.

Help is needed?

thank

UPDATE

The following steps do not work due to compilation errors. Errors come from the second line of formatLines. Type allocation causes errors. Need help here.

formatLine :: (Name,Price)->IO()
formatLine (a,b) = putStrLn (a ++ dots ++ p) 
                   where
                   x=(length a)
                   p=(formatPence b)
                   y=length p  
                   z=lineLength-(x+y)
                   dots = printDots z 


formatLines :: [(Name,Price)]->IO()
formatLines []= ""
formatLines (a:x) = formatLines x ++ formatLine a
+3
source share
4 answers

, IO. . , lineLength, .

formatLine :: Int -> (Name,Price) -> String
formatLine linelength (name, price) =  name ++ dotfill ++ showprice 
  where 
  showprice :: String
  showprice = formatPence price
  extra :: Int
  extra = linelength - length (name ++ showprice)
  dotfill :: String
  dotfill = replicate extra '.'

formatLines :: Int -> [(Name, Price)] -> String
formatLines linelength []= ""
formatLines linelength (first:rest) = 
  (formatLine linelength first ++ "\n") ++ formatLines linelength rest

standardPrint :: [(Name, Price)] -> IO ()
standardPrint listing = putStrLn (formatLines 50 listing)

fileAwayPrices :: FilePath -> [(Name,Price)] -> IO()
fileAwayPrices filename listing = writeFile filename (formatLines 70 listing)

testlist :: [(Name,Price)]
testlist = [("oats",344),("barley", 299),("quinoa",599)]

-- *Main> standardPrint testlist
-- oats...........................................344
-- barley.........................................299
-- quinoa.........................................599


type Name = String
type Price = Integer
formatPence n = show n
+1

++ "\n" ; ', , '.

@marcog, putStr ( putStrLn). :

Hugs> putStr (show 4 ++ "\n")
4

Hugs> putStrLn (show 4 ++ "\n")
4


Hugs> print (show 4 ++ "\n")
"4\n"

( , Hugs .)

+3

, , print x, putStrLn (show x). show x \ n. putStrLn x putStr x, .

, , .

+2

: , formatLines .

formatLines :: [(Name,Price)]->IO()
formatLines [] = return ()
formatLines (a:x) = formatLines x >> formatLine a

A more concise way of writing:

formatLines :: [(Name,Price)]->IO()
formatLines = mapM_ formatLine . reverse
+1
source

Source: https://habr.com/ru/post/1782909/


All Articles