小编use*_*228的帖子

将IO输出收集到列表中

如何SDL.pollEvent :: IO Event在输出结束之前发出多个调用SDL.NoEvent并将所有结果收集到列表中?

在命令性的术语中,这样的事情:

events = []
event = SDL.pollEvent
while ( event != SDL.NoEvent ) {
        events.add( event )
        event = SDL.pollEvent
}
Run Code Online (Sandbox Code Playgroud)

io monads haskell

7
推荐指数
1
解决办法
788
查看次数

类型签名"也许"不喜欢"Just [Event]"

我还在学习Haskell,需要帮助进行类型推断!

使用包SDL和Yampa我从以下类型获得以下类型签名FRP.Yampa.reactimate:

(Bool -> IO (DTime, Maybe a))
Run Code Online (Sandbox Code Playgroud)

我想用它来:

myInput :: Bool -> IO (DTime, Maybe [SDL.Event])
myInput isBlocking = do
    event <- SDL.pollEvent
    return (1, Just [event])
...
reactimate myInit myInput myOutput mySF
Run Code Online (Sandbox Code Playgroud)

但它说

Couldn't match expected type `()'
       against inferred type `[SDL.Event]'
  Expected type: IO (DTime, Maybe ())
  Inferred type: IO (DTime, Maybe [SDL.Event])
In the second argument of `reactimate', namely `input'
In the expression: reactimate initialize input output process

我以为Maybe a允许我使用任何东西,甚至SDL.Event列表?为什么 …

haskell types

3
推荐指数
1
解决办法
276
查看次数

简单的putStrLn在Haskell/Yampa中用箭头语法

我正在使用Haskell和使用箭头语言扩展的Yampa FRP库.

如何在SF中执行简单的putStrLn?

mySF = proc x -> do
    y <- identity -< x*x
    putStrLn "Hello World!" ++ show y
    returnA -< y
Run Code Online (Sandbox Code Playgroud)

箭头语法抱怨表达式不是箭头(当然),但即使使用箭头我也没有输出

 output <- identity -< putStrLn "Hello World!"
Run Code Online (Sandbox Code Playgroud)

haskell arrows frp

3
推荐指数
2
解决办法
901
查看次数

实现相同功能的不同类型的映射列表?

我想将一个函数应用于列表中的每个元素(map),但元素可能有不同的类型,但都实现了相同的函数(这里是"putOut"),就像一个接口.但是,我无法创建此"接口"类型的列表(此处为"可输出").

如何映射实现相同功能的不同类型的列表?

import Control.Monad

main :: IO ()
main = do
 mapM_ putOut lst
 where
  lst :: [Outputable] -- ERROR: Class "Outputable" used as a type
  lst = [(Out1 1),(Out2 1 2)]

class Outputable a where
 putOut :: a -> IO ()

-- user defined:

data Out1 = Out1 Int deriving (Show)
data Out2 = Out2 Int Int deriving (Show)

instance Outputable Out1 where
 putOut out1 = putStrLn $ show out1

instance Outputable Out2 where
 putOut out2 = putStrLn $ …
Run Code Online (Sandbox Code Playgroud)

haskell types interface map

3
推荐指数
2
解决办法
1509
查看次数

标签 统计

haskell ×4

types ×2

arrows ×1

frp ×1

interface ×1

io ×1

map ×1

monads ×1