标签: do-notation

在where子句下做声明

我想转换IO [String][String]具有<-约束力.但是,我需要do在一个where声明中使用一个块来做到这一点,但是Haskell一直抱怨缩进.这是代码:

decompEventBlocks :: IO [String] -> IO [[String]]
decompEventBlocks words
 | words' /= [] = block : (decompEventBlocks . drop $ (length block) words')
 | otherwise = []
  where 
   do
    words' <- words
    let block = (takeWhile (/="END") words')
Run Code Online (Sandbox Code Playgroud)

这是什么原因?我们如何dowhere声明中使用块?而且,我们是否有机会在警卫面前发表一些声明?

monads haskell scope functional-programming do-notation

-1
推荐指数
1
解决办法
432
查看次数

do块中的if语句给出错误消息

我正在尝试制作一个非常简单的类似蛇的游戏,如果您尝试使用已经访问过的斧头坐标,则会输掉该游戏。

到目前为止,这是起作用的代码(您可以使用箭头键移动播放器1并使用wasd移动播放器2):

import UI.NCurses

main :: IO ()    
main = runCurses $ do
    w <- defaultWindow
    updateWindow w $ do
        drawBorder Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
    render
    loop w [] 1 1 0 0 10 10 0 0

loop :: Window -> [(Integer, Integer)] -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer ->  Curses ()
loop w list p1x p1y p1oldDx p1oldDy p2x p2y p2oldDx p2oldDy = do
    e …
Run Code Online (Sandbox Code Playgroud)

haskell if-statement ncurses do-notation io-monad

-1
推荐指数
1
解决办法
61
查看次数

Haskell中的“无法将类型'[]'与'IO'匹配”错误

我正在尝试用Points(我创建的数据类型)列出一个列表,这个想法是在每次迭代中添加一个元素。出了点问题。

我已经尝试过p了,myLoop但它似乎也不起作用。

main = myLoop 
myLoop  = do 
            let p = []
            done <- isEOF
            if done
              then putStrLn ""
              else do inp <- getLine
                      let (label:coord) = words inp
                      p ++  [Point label (map getFloat coord)]
                      -- print (pointerList)
                      myLoop 
Run Code Online (Sandbox Code Playgroud)

我得到这个输出

trabalho.hs:30:23: error:
    • Couldn't match type ‘[]’ with ‘IO’
      Expected type: IO Point
        Actual type: [Point]
    • In a stmt of a 'do' block:
        p ++ [Point label (map getFloat coord)]
      In the expression: …
Run Code Online (Sandbox Code Playgroud)

haskell list type-mismatch do-notation io-monad

-1
推荐指数
1
解决办法
76
查看次数

理解 Haskell (&lt;-) 语法糖

我正在尝试使用 Cassava 解析 CSV 文件。我想要一个函数,Nothing如果解析不成功Just (V.Vector (String, String, String))则返回,否则返回。

我正在使用下面的代码:

  {-# LANGUAGE ScopedTypeVariables #-}
module Lib
    ( someFunc
    ) where

import qualified Data.ByteString.Lazy as BL
import Data.Csv
import qualified Data.Vector as V

type Dataset = (String, String, String)
someFunc :: Maybe (V.Vector Dataset)
someFunc = do
    csvData <- BL.readFile "TAEE3.SA.csv"
    case decode HasHeader csvData :: Either String (V.Vector (String, String, String)) of
      Left a -> Nothing
      Right v -> Just v
Run Code Online (Sandbox Code Playgroud)

错误是:

  • Couldn't match type …
Run Code Online (Sandbox Code Playgroud)

monads haskell do-notation

-1
推荐指数
1
解决办法
157
查看次数