如何在带有 IOMonad 实例的类型中递归使用纯函数?

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 - How can I use pure functions inside IO functions? 中的解决方案,但我仍然无法递归执行此操作。

依赖关系:

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
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

<$> 允许您将单子输入传递给具有非单子输出的函数。在您的情况下,您想将单子输入传递给具有单子输出的函数,因此您需要 =<< 。出于优先原因,您现在还需要括号,setScrollHPos height >> (scrollUntilEnd0 height =<< getHeight).