[英]how to pass a string around a list of strings in haskell
How would I get this piece of code to accept a list of strings and output a frame around the outside. 我如何获得这段代码来接受字符串列表并在外部输出框架。 I understand the concept but just cannot execute the code in the final frame function.
我理解这个概念,但是无法在最终框架函数中执行代码。
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
I have worked on this all day and come up with this but there's still some bugs I need to work out 1. When I pass the border string into the frameM function, If I was to pass say "SS" is there any way I could make the S'S frame on top of each other,istead of side by side so the more letters i put into the first argument the bigger the total perimeter of the frame gets? 我整天都在努力,想出了这个问题,但仍然有一些错误需要解决。1.当我将边框字符串传递给frameM函数时,如果我要传递,请说“ SS”有什么办法使S形框架彼此并排放置,而不是并排放置,因此我在第一个参数中输入的字母越多,框架的总周长越大? heres what I've done:
这是我所做的:
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
I believe the type of your frame
should be frame :: String -> IO ()
— it takes a string a puts a "framed" version of it to stdout
. 我相信
frame
的类型应该是frame :: String -> IO ()
-它需要一个字符串,并将其“框架”版本放入stdout
。 Then you don't need map putStrLn
and can just use putStrLn
. 然后,您就不需要
map putStrLn
而只需使用putStrLn
。
Now, consider this line: 现在,考虑以下这一行:
putStrLn (bar m) ++ "\n" ++ textshown ++ "\n" ++ (bar m)
you are calling putStrLn (bar m)
and then trying to append some stuff to the result of that (hint: use parentheses or $
). 您正在调用
putStrLn (bar m)
,然后尝试将一些内容附加到该结果(提示:使用括号或$
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.