Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何正确选择要连接的USB设备?_Haskell - Fatal编程技术网

Haskell 如何正确选择要连接的USB设备?

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

我对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 (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