加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 13:05:05 所属栏目:Python 来源:网络整理
导读:有时当我想使用wget时,我最终会用 Python打印一堆行: for i in range(25):... print "http://www.theoi.com/Text/HomerOdyssey",i,".html"... http://www.theoi.com/Text/HomerOdyssey 0 .htmlhttp://www.theoi.com/Text/HomerOdyssey 1 .htmlhttp://www.th
有时当我想使用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 `='

解决方法

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

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

现在有些理论:

> putStrLn是一个只接受一个参数的函数. Python,Perl等会将您提供的所有参数打印出来并将其转换为单个字符串.在Haskell你必须自己做.> mapM_有两个参数.其次是列表,首先是mapM_依次传递列表的每个元素的函数.我们在这里传递的函数是一个匿名函数(就像Python中的lambda).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读