3

有时当我想使用 时wget,我最终会用 Python 打印一堆行,如下所示:

>>> for i in range(25):
...   print "http://www.theoi.com/Text/HomerOdyssey", i, ".html"
... 
http://www.theoi.com/Text/HomerOdyssey 0 .html
http://www.theoi.com/Text/HomerOdyssey 1 .html
http://www.theoi.com/Text/HomerOdyssey 2 .html
http://www.theoi.com/Text/HomerOdyssey 3 .html
http://www.theoi.com/Text/HomerOdyssey 4 .html
http://www.theoi.com/Text/HomerOdyssey 5 .html
http://www.theoi.com/Text/HomerOdyssey 6 .html
http://www.theoi.com/Text/HomerOdyssey 7 .html
http://www.theoi.com/Text/HomerOdyssey 8 .html
http://www.theoi.com/Text/HomerOdyssey 9 .html
http://www.theoi.com/Text/HomerOdyssey 10 .html
http://www.theoi.com/Text/HomerOdyssey 11 .html
http://www.theoi.com/Text/HomerOdyssey 12 .html
http://www.theoi.com/Text/HomerOdyssey 13 .html
http://www.theoi.com/Text/HomerOdyssey 14 .html
http://www.theoi.com/Text/HomerOdyssey 15 .html
http://www.theoi.com/Text/HomerOdyssey 16 .html
http://www.theoi.com/Text/HomerOdyssey 17 .html
http://www.theoi.com/Text/HomerOdyssey 18 .html
http://www.theoi.com/Text/HomerOdyssey 19 .html
http://www.theoi.com/Text/HomerOdyssey 20 .html
http://www.theoi.com/Text/HomerOdyssey 21 .html
http://www.theoi.com/Text/HomerOdyssey 22 .html
http://www.theoi.com/Text/HomerOdyssey 23 .html
http://www.theoi.com/Text/HomerOdyssey 24 .html
>>> 

我可以将该输出粘贴到一个新文件中,删除空格,然后使用wget -i.

但我厌倦了 Python。

我想学习 Haskell。

尽管花了 10 分钟试图从 开始做同样的事情ghci,但我没有进一步前进。

这就是我的尝试的样子:

alec@ROOROO:~/oldio$ ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> putStrLn

<interactive>:1:1:
    No instance for (Show (String -> IO ()))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for (Show (String -> IO ()))
    In a stmt of an interactive GHCi command: print it
Prelude> putStrLn "hey"
hey
Prelude> putStrLn "hey" [1..10]

<interactive>:1:1:
    The function `putStrLn' is applied to two arguments,
    but its type `String -> IO ()' has only one
    In the expression: putStrLn "hey" [1 .. 10]
    In an equation for `it': it = putStrLn "hey" [1 .. 10]
Prelude> putStrLn "hey" snd [1..10]

<interactive>:1:1:
    The function `putStrLn' is applied to three arguments,
    but its type `String -> IO ()' has only one
    In the expression: putStrLn "hey" snd [1 .. 10]
    In an equation for `it': it = putStrLn "hey" snd [1 .. 10]
Prelude> putStrLn "hey" $ snd [1..10]

<interactive>:1:1:
    The first argument of ($) takes one argument,
    but its type `IO ()' has none
    In the expression: putStrLn "hey" $ snd [1 .. 10]
    In an equation for `it': it = putStrLn "hey" $ snd [1 .. 10]
Prelude> "hello"
"hello"
Prelude> "hello" ++ "world"
"helloworld"
Prelude> "hello" ++ [1..10] ++ " world"

<interactive>:1:16:
    No instance for (Num Char)
      arising from the literal `10'
    Possible fix: add an instance declaration for (Num Char)
    In the expression: 10
    In the first argument of `(++)', namely `[1 .. 10]'
    In the second argument of `(++)', namely `[1 .. 10] ++ " world"'
Prelude> "hello" ++ print [1..10] ++ " world"

<interactive>:1:12:
    Couldn't match expected type `[Char]' with actual type `IO ()'
    In the return type of a call of `print'
    In the first argument of `(++)', namely `print [1 .. 10]'
    In the second argument of `(++)', namely
      `print [1 .. 10] ++ " world"'
Prelude> print [1..10]
[1,2,3,4,5,6,7,8,9,10]
Prelude> map ("hello") [1..10]

<interactive>:1:6:
    Couldn't match expected type `a0 -> b0' with actual type `[Char]'
    In the first argument of `map', namely `("hello")'
    In the expression: map ("hello") [1 .. 10]
    In an equation for `it': it = map ("hello") [1 .. 10]
Prelude> greeting :: String --> Int  -> [String, Int]

<interactive>:1:39: parse error on input `,'
Prelude> greeting :: String --> Int  -> [(String), (Int)]

<interactive>:1:41: parse error on input `,'
Prelude> greeting :: String -> Int  -> [(String), (Int)]

<interactive>:1:40: parse error on input `,'
Prelude> greeting :: String -> Int  -> [(String) (Int)]

<interactive>:1:1: Not in scope: `greeting'
Prelude> foreach [1..24] print

<interactive>:1:1: Not in scope: `foreach'
Prelude> import Data.IORef
Prelude Data.IORef> foreach [1..24] print

<interactive>:1:1: Not in scope: `foreach'
Prelude Data.IORef> foreach = flip mapM_

<interactive>:1:9: parse error on input `='
4

4 回答 4

13
for i in range(25):
...   print "http://www.theoi.com/Text/HomerOdyssey", i, ".html"

变成:

导入 Control.Monad

这样我们就可以:

 forM_ [1..25] $ \i ->
     putStrLn $ "http://www.theoi.com/Text/HomerOdyssey" ++ show i ++ ".html"
于 2012-05-22T14:17:46.820 回答
9
mapM_ (\i -> putStrLn (concat ["http://www.theoi.com/Text/HomerOdyssey", show i, ".html"])) [0..24]

作为奖励,这不会打印任何空格。

现在一些理论:

  • putStrLn是一个接受单个参数的函数。Python、Perl 等会吞下您提供的所有参数print并将其转换为单个字符串。在 Haskell 中,你必须自己做。
  • mapM_有两个参数。第二个列表,第一个函数mapM_依次传递列表的每个元素。我们在这里传递的函数是一个匿名函数(就像 Python 中的 lambda)。
于 2012-05-22T14:17:07.537 回答
3

考虑使用列表推导:

mapM_ putStrLn ["http://www.theoi.com/Text/HomerOdyssey" ++ show i ++ ".html" | i <- [0..24]]
于 2012-05-22T22:28:18.910 回答
1

这不完全是一个答案,但评论太长了。我认为关于 ghci 的几句话会有很大帮助。

在 ghci 中,您可以使用:t:info来分别显示某物的类型和有关某物的信息。

Prelude> :t putStrLn
putStrLn :: String -> IO ()
Prelude> :info putStrLn
putStrLn :: String -> IO ()     -- Defined in `System.IO'
Prelude> :t putStrLn "Hello, World"
putStrLn "Hello, World" :: IO ()

:t很有用,因为您可以使用它来显示任意表达式的类型,而这:info不会。但:info可以提供关于没有类型的事物的信息,例如类型本身或类型构造函数:

Prelude> :info IO
newtype IO a
  = GHC.Types.IO (GHC.Prim.State# GHC.Prim.RealWorld
                  -> (# GHC.Prim.State# GHC.Prim.RealWorld, a #))
    -- Defined in `GHC.Types'
instance Monad IO -- Defined in `GHC.Base'
instance Functor IO -- Defined in `GHC.Base'

当你试图弄清楚如何让 ghci 接受某些东西时,:t它会非常有用。

接下来,意识到 ghci 提示符在 IO 中为您提供了逐行的 do-notation。如果您输入 typeIO x的内容,则当您按 Enter 时,将评估该语句。如果你想绑定那个x,使用 do-notation 箭头。如果要创建新绑定(即创建新函数或标识符),请使用let. 如果你输入一个不是 IO 的表达式,ghci 会尝试给你,如果没有定义实例show,这将不起作用。Show

Prelude> putStrLn "Hello, World"
Hello, World
Prelude> :t getChar
getChar :: IO Char
Prelude> x <- getChar
yPrelude> 
Prelude> show x
"'y'"
Prelude> let g = 10 :: Int
Prelude> :t g
g :: Int
Prelude> show g
"10"
Prelude> g
10

请注意show gg不一样。您可能可以从我上面概述的行为中找出原因。

于 2012-05-23T12:04:19.367 回答