Haskell 如何正确选择要连接的USB设备?
我对USB驱动程序和Haskell都是新手。我正在尝试使用System.USB连接到Tomu(一种适合USB端口的ARM微控制器)。我试过这个例子:,但它已经过时好几年了。我正试图在USB上创建hello world,但遇到了类型错误 以下代码起作用:Haskell 如何正确选择要连接的USB设备?,haskell,Haskell,我对USB驱动程序和Haskell都是新手。我正在尝试使用System.USB连接到Tomu(一种适合USB端口的ARM微控制器)。我试过这个例子:,但它已经过时好几年了。我正试图在USB上创建hello world,但遇到了类型错误 以下代码起作用: module Lib where import System.USB import System.USB.IO import Data.Vector import Data.Maybe getOneDesc :: Int -> IO
module Lib where
import System.USB
import System.USB.IO
import Data.Vector
import Data.Maybe
getOneDesc :: Int -> IO (VendorId, ProductId)
getOneDesc n = do
usbConn <- newCtx
devList <- getDevices usbConn
f <- getDeviceDesc $ devList ! n
let f0 = deviceVendorId f
let f1 = deviceProductId f
return (f0, f1)
我想我应该能做这样的事情:
isThisDeviceTheOneIWant :: Int -> VendorId -> ProductId -> Bool
isThisDeviceTheOneIWant n a b = do
(x, y) <- getOneDesc n
return (x == a) && (y == b)
此设备是否为OneIwant::Int->VendorId->ProductId->Bool
该设备是否为我想要的设备
(x,y)您的getOneDesc
是一个IO(VendorId,ProductId)
,这意味着您的返回(x==a)&&(y==b)
的结果类型具有类型IO Bool
。因此,您应该更改函数的类型。还必须在传递给return
的参数周围添加括号(因为在Haskell中,return
不是关键字,只是一个普通函数)
或使用与fmap
相同的方法:
isThisDeviceTheOneIWant :: Int -> VendorId -> ProductId -> IO Bool
isThisDeviceTheOneIWant n a b = ((a, b) ==) <$> getOneDesc n
isThisDeviceTheOneIWant::Int->VendorId->ProductId->IO Bool
Isthis Device TheOneIwant n a b=((a,b)=)getOneDesc n
如果你想喜欢,你可以编写Isthis Device TheOneIwant n a b=((a,b)=)getOneDesc n
。我向专家推荐这一点,这样读者就不必再仔细检查这个简单的do
块中是否有鬼鬼祟祟的事情发生;但对于初学者来说,这可能有点过分。
isThisDeviceTheOneIWant :: Int -> VendorId -> ProductId -> IO Bool
isThisDeviceTheOneIWant n a b = do
(x, y) <- getOneDesc n
return ((x == a) && (y == b))
isThisDeviceTheOneIWant :: Int -> VendorId -> ProductId -> IO Bool
isThisDeviceTheOneIWant n a b = fmap ((a, b) ==) (getOneDesc n)
isThisDeviceTheOneIWant :: Int -> VendorId -> ProductId -> IO Bool
isThisDeviceTheOneIWant n a b = ((a, b) ==) <$> getOneDesc n