关于我想在Haskell做的事情,我有一个简短的问题。我基本上要实现的目标是制作一个从1到特定值y的整数列表。像[1..y],并打印此列表,每个数字之间都有空格
假设我有[1..8]
我想要的输出是(“ _”代表空格):
_1_2_3_4_5_6_7_8
Run Code Online (Sandbox Code Playgroud)
我玩了一些不同的东西,但是没有运气
这基本上是我到目前为止所获得的
printLst :: [Int] -> String
printLst (x:xs) = " " ++ putStr (show x) >> printLst xs
Run Code Online (Sandbox Code Playgroud)
我一直在网上搜索以找到解决方案,但是没有找到任何可以帮助我完成此任务的方法。
非常感谢帮助
我涉嫌Haskell,偶然发现路途颠簸
我想编写一个AI用来在简单棋盘游戏中选择一行的函数。所述游戏中的棋盘由整数列表表示。像这样:
board = [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
板上一行的数字的索引+ 1及其Int本身就是该行上剩余的块数。我旨在使此功能起作用的方法是,首先在列表中找到最大的数字,然后以的形式返回此数字(索引+ 1)IO Int。
这是我在努力的地方,因为我似乎无法在网上找到任何好的答案
到目前为止,这是我正在使用的:
-- Returns index of row with largest number along the number itself
aiHelper :: Board -> (Int, Int)
aiHelper xs = maximumBy (comparing fst) (zip xs [1..])
-- Returns row with largest number as IO Int
aiRow :: Board -> IO Int
aiRow xs = do
let y = snd $ aiHelper xs
return $ y
Run Code Online (Sandbox Code Playgroud)
我不太确定这段代码是否符合我的期望,是否有更简单,更干净的代码解决方案?
我正在处理一些Haskell代码,偶然发现了一个问题。基本上,我正在编写一个代码块的情况,以检查由用户提供的输入,该输入检查用户输入是否为任何可接受的字符以及整数
我尝试在第一个case块之后编写另一个case块,但无法解决。我试图编写一个if-else块,以检查在case块之前是否输入为int,但这也失败了
这是从函数中间获取的代码块:
if player == 1 then
do newline
putStr "Nim: r a / ? / q > "
inp <- getLine
let inputs = words inp
h = head inputs
case h of
"q" -> do
play
"?" -> do
putStrLn "r is the row and a is amount to remove"
putStrLn "The person to remove the last piece wins"
play board player
-- The case below is the one that I can't seem to make work
-- …Run Code Online (Sandbox Code Playgroud)