簡體   English   中英

如何在帶有 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 中的解決方案 - 如何在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM