2016-06-13 27 views
0
{-# LANGUAGE ScopedTypeVariables #-} 

module Main where 
    import Control.Applicative ((<$>)) 
    import Data.ByteString (ByteString) 
    import qualified Data.ByteString as B 
    import qualified Data.ByteString.Char8 as C 
    import qualified Data.Vector.Unboxed as V 
    import qualified Data.Vector as VB 
    import qualified Data.List as L 

    (|>) :: a -> (a -> b) -> b 
    x |> f = f x 

    main :: IO() 
    main = 
    let 
     parse (s :: ByteString) = s |> L.unfoldr (C.readInt . C.dropWhile (== ' ')) 
     solve (l :: Int) (ars :: VB.Vector ByteString) = 
      let 
       (n :: V.Vector (Int, Int)) = V.fromList [(x,y) | x <- [0..l-1], y <- [x+1..l-1]] 
      in 
       n 
    in do 
     [n,l :: Int] <- parse <$> B.getLine 
     (ars :: VB.Vector ByteString) <- VB.replicateM n B.getLine 
     ars 
     |> solve l 
     |> print 

編譯器抱怨l由於某種原因不在這裏。這是爲什麼?在Haskell中爲什麼|>運算符在這個例子中給出了範圍錯誤?

Not in scope: `l' 

另外的代碼看起來像這樣的原因是因爲我剛剛想出ScopedTypeVariables擴展做什麼。它應該在適當的語言和初學者教程中。在過去的幾周裏,這會爲我省去很多痛苦。

+0

這不是我的錯誤。你能給你正在測試的實際程序以及你得到的編譯器錯誤嗎? – pdexter

+0

對不起。我編輯了示例,以便觸發錯誤。同樣,當我將它寫成「ars |>解決打印」問題時,這個例子似乎有效。 –

+0

例如,您仍然缺少進口產品。 – pdexter

回答

1

您的縮進是錯誤的。以下任一工作。

{-# LANGUAGE ScopedTypeVariables #-} 
import Data.ByteString as B 
import qualified Data.Vector.Unboxed as V 
import qualified Data.Vector as VB 
import Data.List as L 
import Data.ByteString.Char8 as C 

(|>) :: a -> (a -> b) -> b 
x |> f = f x 

main :: IO() 
main = 
    let 
    parse (s :: ByteString) = s |> L.unfoldr (C.readInt . C.dropWhile (== ' ')) 
    solve (l :: Int) (ars :: VB.Vector ByteString) = 
     let 
     (n :: V.Vector (Int, Int)) = V.fromList [(x,y) | x <- [0..l-1], y <- [x+1..l-1]] 
     in 
     n 
    in do 
    [n,l :: Int] <- parse <$> B.getLine 
    (ars :: VB.Vector ByteString) <- VB.replicateM n B.getLine 
    ars |> solve l |> print 

-

{-# LANGUAGE ScopedTypeVariables #-} 
import Data.ByteString as B 
import qualified Data.Vector.Unboxed as V 
import qualified Data.Vector as VB 
import Data.List as L 
import Data.ByteString.Char8 as C 

(|>) :: a -> (a -> b) -> b 
x |> f = f x 

main :: IO() 
main = 
    let 
    parse (s :: ByteString) = s |> L.unfoldr (C.readInt . C.dropWhile (== ' ')) 
    solve (l :: Int) (ars :: VB.Vector ByteString) = 
     let 
     (n :: V.Vector (Int, Int)) = V.fromList [(x,y) | x <- [0..l-1], y <- [x+1..l-1]] 
     in 
     n 
    in do 
    [n,l :: Int] <- parse <$> B.getLine 
    (ars :: VB.Vector ByteString) <- VB.replicateM n B.getLine 
    ars |> 
     solve l |> 
     print 
+0

呃,對不起,請看看編輯。 'ars |>解決l |> print'應該在3行,我不小心粘貼了正確的東西。現在我看到我有一個簡單的縮進問題。是的,就是這樣。 –

相關問題