簡體   English   中英

如何在haskell的字符串列表中傳遞字符串

[英]how to pass a string around a list of strings in haskell

我如何獲得這段代碼來接受字符串列表並在外部輸出框架。 我理解這個概念,但是無法在最終框架函數中執行代碼。

minusdots :: Int -> String
minusdots 1    = "-."
minusdots n
   | n > 1     = "-." ++ (minusdots (n-1))
   | otherwise = error "please enter greater than 1"


bar :: Int -> String
bar n
    | even n    = minusdots (div n 2)
    | otherwise = (minusdots (div n 2)) ++ ['-']


 frame :: [String] -> IO String
 frame text = map putStrLn (bar m) ++ "\n" ++ textshown ++ "\n" ++ (bar m)
    where
    textshown = "- " ++ text ++ " -"
    m         = length textshown

我整天都在努力,想出了這個問題,但仍然有一些錯誤需要解決。1.當我將邊框字符串傳遞給frameM函數時,如果我要傳遞,請說“ SS”有什么辦法使S形框架彼此並排放置,而不是並排放置,因此我在第一個參數中輸入的字母越多,框架的總周長越大? 這是我所做的:

minusdots ::  Int -> String -> String
minusdots 1  a =  a
minusdots  n a
   | n > 1     =  a ++ (minusdots (n-1) a)
   | otherwise = error "argument not addmissible"


bar :: String -> Int -> String
bar s n
   | even n    = minusdots (div n 2) s 
   | otherwise = (minusdots (div n 2) s) ++ s


frameM :: String -> String -> String
frameM a text = (bar a m) ++ "\n" ++ textshown ++ "\n" ++ (bar a m)  
    where
    textshown = b ++ text ++ b
    m         = length textshown
    b         = a

我相信frame的類型應該是frame :: String -> IO () -它需要一個字符串,並將其“框架”版本放入stdout 然后,您就不需要map putStrLn而只需使用putStrLn

現在,考慮以下這一行:

putStrLn (bar m) ++ "\n" ++ textshown ++ "\n" ++ (bar m)

您正在調用putStrLn (bar m) ,然后嘗試將一些內容附加到該結果(提示:使用括號或$ )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM