English 中文(简体)
如何在本法中不出现任何错误的情况下使用 lo?
原标题:How can i use the loop without any error in this code?

这是一项方案,使GHC从用户那里获得一定数量的零,然后形成一份由用户再次提供的数字清单。 该法典显示了某些 par形错误。 如何消除这一错误?

import System.IO

main = do 
   putStrLn "Please enter the number"
   number <- readLn :: IO Int
   putStrLn $ "The num is:" ++ show number
   loop number
   xs <- sequence [readLn :: IO Int]
   putStrLn xs

loop:: Int -> IO ()
loop n = if 0==n then return () else return loop (n-1)
问题回答

除其他答复外:

无需写假。

import System.IO

main = do 
   putStrLn "Please enter the number"
   number <- readLn :: IO Int
   putStrLn $ "The num is:" ++ show number
   xs <- sequence (replicate number (readLn :: IO Int))  -- pay attention to this line
   print xs

因此,我们开始采用<代码>readLn>replicate ,列出<代码>>>/编号>。 readLns. (根据您的观点,您可能认为这是切身之地)。)

轨道:sequence 采取综合行动清单,把它变成一个大型综合行动。 每个<代码>readLn反过来发生,收益值则在一份清单中收集和返还。

你的法典有三处错误。 在《全球健康标准》中进行编辑并首先提供以下信息:

temp.hs:9:13
    Couldn t match expected type `Char  with actual type `Int 
    Expected type: String
      Actual type: [Int]
    In the first argument of `putStrLn , namely `xs 
    In the expression: putStrLn xs

这一点非常清楚。 。 Int s. 因此,仅使用<条码>xs而不是<条码>。 解决这一问题(print = pageStrLn . show)。

接下来的两个问题是:

    temp.hs:13:38:
        No instance for (Monad ((->) t0))
          arising from a use of `return 
        Possible fix: add an instance declaration for (Monad ((->) t0))
        In the expression: return loop (n - 1)
        In the expression:
          if 0 == n then return () else return loop (n - 1)
        In an equation for `loop :
            loop n = if 0 == n then return () else return loop (n - 1)

temp.hs:13:45:
    Couldn t match expected type `IO () 
                with actual type `Int -> IO () 
    In the first argument of `return , namely `loop 
    In the expression: return loop (n - 1)
    In the expression:
      if 0 == n then return () else return loop (n - 1)

The problem is in the types. loop is of type Int -> IO (). So the first branch of the function is alright, because you return (). However, in the else branch, you return something completely different, because return is not some built-in statement of the language, but a normal function. So return loop (n - 1) first lifts your loop function into the monad and than applies it to (n - 1).

相反,你想要的是:

loop n = if n == 0 then return () else loop (n - 1)

又注意到你不需要<代码>0=N,因为没有办法意外使用转让而不是平等比较(没有汇编)。

Edit:正如其他答复所指出的,loop 确实没有做任何事情——这只是说自己是1倍,然后是返回()。

如果我正确理解你的意图,你就打算建立一个“徒劳”的建筑,以开展行动n,并编制一份结果清单。 阁下的守则

loop number
xs <- sequence [readLn :: IO Int]

但是,这两份声明是分开的;你需要改为send。 页: 1

xs <- loop number readLn :: IO [Int]

现在当然,你们需要改写<条码>。

loop :: Int -> IO a -> IO [a]
loop 0 action = return []  -- don t perform the action
loop n action = do
  x <- action              -- perform the action once
  xs <- loop (n-1) action  -- then perform it (n-1) times
  return (x:xs)            -- produce the resultant list

在此,我用对应方式而不是if n=0书写。 我可以轻而易举地用“如果”建筑书写,但我个人往往发现这些建筑。

但是,如果等到,标准图书馆可能已经存在这样的情况。 停止......罗马教廷时代! Hoogling the category signature for we new loop, ,我们发现replicate/code>。

xs <- replicateM number readLn :: IO [Int]




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?