bar*_*ron 1 string haskell char
我正在寻找一个函数(让我们将其命名为菱形),通过仅定义其对角线的长度来"绘制"菱形"对象"(例如:如下所示,因此在这种情况下对角线的长度为5 "*"但只有3个可见):
Main> diamond 5
Output:
*
***
*****
***
*
Run Code Online (Sandbox Code Playgroud)
我有这些功能:
stars x
| (x mod 2 == 1) = (firstHalf x) ++ [x] ++ reverse(firstHalf x)
| otherwise = (firstHalf x) ++ reverse(firstHalf x)`
firstHalf x = take (x div 2) (iterate (2+) 1)
space x = map (div 2) (map ((x+1)-) (stars x))
Run Code Online (Sandbox Code Playgroud)
示例输出:
space 5 = [2,1,0,1,2]
stars 5 = [1,3,5,3,1]
Run Code Online (Sandbox Code Playgroud)
此功能可能有效,但我不知道如何解决此错误:
应用程序中输入错误:
diamond x = mapM_ putStrLn $ (replicate (space x) [' ']) ++ (replicate (star x) ['*'])
我猜即使这个错误得到修复,它仍然需要一个可以加入这两个列表的调整来获得所需的结果.有任何想法吗?
您可以通过以下方式生成多个星星replicate:
ghci> replicate 5 '*'
"*****"
Run Code Online (Sandbox Code Playgroud)
您也可以使用任何其他字符执行此操作,例如space(' ').
您可以使用以下方法在屏幕上打印一行putStrLn:
ghci> putStrLn (replicate 5 '*')
*****
Run Code Online (Sandbox Code Playgroud)
这是一个生成上面明星的程序:
main = do
putStrLn $ replicate 2 ' ' ++ replicate 1 '*'
putStrLn $ replicate 1 ' ' ++ replicate 3 '*'
putStrLn $ replicate 0 ' ' ++ replicate 5 '*'
putStrLn $ replicate 1 ' ' ++ replicate 3 '*'
putStrLn $ replicate 2 ' ' ++ replicate 1 '*'
Run Code Online (Sandbox Code Playgroud)
您可以定义与功能=:
hello x = putStrLn $ "Hello " ++ x
main = do
hello "Bob" -- prints "Hello Bob"
Run Code Online (Sandbox Code Playgroud)
(如果你从ghci提示符执行此操作,则必须先说let:例如.let hello x = putStrLn $ "Hello " ++ x)
我不知道为什么我在这个问题上花了那么多时间,但我觉得这样.通常我们想要问题提供一些证据,证明您在寻求帮助之前花了一些时间来解决问题.圣诞节快乐...
我已经把你所有的东西都给了你.现在你必须把它们放在一起.
| 归档时间: |
|
| 查看次数: |
644 次 |
| 最近记录: |