如何在Haskell中表达这个Python for循环?

mag*_*tar 3 python iteration io haskell wget

有时当我想使用时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
>>> 
Run Code Online (Sandbox Code Playgroud)

我可以将该输出粘贴到新文件中,删除空格,然后使用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 `='
Run Code Online (Sandbox Code Playgroud)

Don*_*art 13

for i in range(25):
...   print "http://www.theoi.com/Text/HomerOdyssey", i, ".html"
Run Code Online (Sandbox Code Playgroud)

变为:

导入Control.Monad

这样我们就可以:

 forM_ [1..25] $ \i ->
     putStrLn $ "http://www.theoi.com/Text/HomerOdyssey" ++ show i ++ ".html"
Run Code Online (Sandbox Code Playgroud)

  • `$`运算符用于避免括号.它包含了右边的任何内容.它避免了类似Lisp的剪辑. (3认同)

dav*_*420 9

mapM_ (\i -> putStrLn (concat ["http://www.theoi.com/Text/HomerOdyssey", show i, ".html"])) [0..24]
Run Code Online (Sandbox Code Playgroud)

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

现在有些理论:

  • putStrLn是一个采用单个参数的函数.Python,Perl等会将你提供给的所有参数都搞砸print并将其转换为单个字符串.在Haskell你必须自己做.
  • mapM_有两个论点.其次是列表,首先是mapM_依次传递列表的每个元素的函数.我们在这里传递的函数是一个匿名函数(就像Python中的lambda).