[英]How to use pure functions recursively in types with IOMonad instances?
我收到錯誤:
Main.hs:38:22: error:
• Couldn't match type ‘WD ()’ with ‘()’
...
• In the expression:
setScrollHPos height >> scrollUntilEnd0 height <$> getHeight
在哪里:
setScrollHPos :: Integer -> WD ()
scrollUntilEnd0 :: Integer -> Integer -> WD ()
scrollUntilEnd0 lastHeight height
| lastHeight < height = setScrollHPos height >> scrollUntilEnd0
WD
有一個IOMonad
實例。
我正在嘗試對 I/O monad 的值使用純函數,因此,我嘗試了 Stack Overflow 問題Haskell 中的解決方案 - 如何在 IO 函數中使用純函數? ,但我仍然無法遞歸地做到這一點。
dependencies:
- base >= 4.7 && < 5
- http-client
- http-client-tls
- bytestring
- webdriver
{-# LANGUAGE OverloadedStrings #-}
module Module (main) where
import Test.WebDriver
import qualified Test.WebDriver.JSON as JSON
import System.IO
import Control.Monad.IO.Class (liftIO)
firefoxConfig :: WDConfig
firefoxConfig = hilf $ useBrowser firefox defaultConfig
where
hilf m = m { wdHTTPRetryCount = 100 }
main :: IO ()
main = runSession firefoxConfig $
openPage "https://duckduckgo.com" >>
scrollUntilEnd >>
liftIO getLine >>
closeSession
where
getHeight :: WD Integer
getHeight = executeJS [] "return document.documentElement.scrollHeight"
setScrollHPos :: Integer -> WD ()
setScrollHPos height = JSON.ignoreReturn $ executeJS [JSArg height]
"(function(h) { window.scrollTo(0, h); })"
scrollUntilEnd0 :: Integer -> Integer -> WD ()
scrollUntilEnd0 lastHeight height
| lastHeight < height = setScrollHPos height >> scrollUntilEnd0 height <$> getHeight
| otherwise = pure ()
scrollUntilEnd :: WD ()
scrollUntilEnd = scrollUntilEnd0 0 <$> getHeight```
Main.hs:34:31: error:
• Couldn't match type ‘WD ()’ with ‘()’
Expected type: WD ()
Actual type: WD (WD ())
• In the expression:
setScrollHPos height >> scrollUntilEnd0 height <$> getHeight
In an equation for ‘scrollUntilEnd0’:
scrollUntilEnd0 lastHeight height
| lastHeight < height
= setScrollHPos height >> scrollUntilEnd0 height <$> getHeight
| otherwise = pure ()
In an equation for ‘someFunc’:
someFunc
= runSession firefoxConfig
$ openPage "https://duckduckgo.com" >> scrollUntilEnd
>> liftIO getLine
>> closeSession
where
getHeight :: WD Integer
getHeight
= executeJS [] "return document.documentElement.scrollHeight"
setScrollHPos :: Integer -> WD ()
setScrollHPos height
= JSON.ignoreReturn
$ executeJS
[JSArg height] "(function(h) { window.scrollTo(0, h);
....
|
34 | | lastHeight < height = setScrollHPos height >> scrollUntilEnd0 heig
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Main.hs:38:22: error:
• Couldn't match type ‘WD ()’ with ‘()’
Expected type: WD ()
Actual type: WD (WD ())
• In the expression: scrollUntilEnd0 0 <$> getHeight
In an equation for ‘scrollUntilEnd’:
scrollUntilEnd = scrollUntilEnd0 0 <$> getHeight
In an equation for ‘someFunc’:
someFunc
= runSession firefoxConfig
$ openPage "https://duckduckgo.com" >> scrollUntilEnd
>> liftIO getLine
>> closeSession
where
getHeight :: WD Integer
getHeight
= executeJS [] "return document.documentElement.scrollHeight"
setScrollHPos :: Integer -> WD ()
setScrollHPos height
= JSON.ignoreReturn
$ executeJS
[JSArg height] "(function(h) { window.scrollTo(0, h);
....
|
38 | scrollUntilEnd = scrollUntilEnd0 0 <$> getHeight
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<$>
允許您將單子輸入傳遞給帶有非單子 output 的 function。在您的情況下,您想將單子輸入傳遞給帶有單子 output 的 function,因此您需要=<<
代替。 出於優先原因,您現在還需要括號,所以需要setScrollHPos height >> (scrollUntilEnd0 height =<< getHeight)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.