2

このコードのカウンターのインクリメントに大きな問題があります。増加しません。 編集:正常に動作するようになりました!

問題がどこにあるのか、どうすれば解決できるのか教えてください。

replaceBasedIdx    ::  String  ->  [String]  ->  String  ->  String
replaceBasedIdx    findStr replaceStrList myText = replaceBasedIdxSub findStr replaceStrList myText 0

replaceBasedIdxSub  ::  String  ->  [String]  ->  String  -> Int -> String
replaceBasedIdxSub findStr replaceStrList myText counter = loop myText 0
  where
    loop [] _ = []
    loop myText counter =
      let (prefix, rest) = splitAt n myText
      in
        if findStr == prefix                                -- found an occurrence?
        then (replaceStrList !! counter) ++ loop rest (counter+1)   -- yes: replace it

        else head myText : loop (tail myText) counter              -- no: keep looking
    n = length findStr

インクリメントはこのコードで問題なく機能します

これはなぜですか?

numStack :: [Integer]
numStack = [20, 45, 150, 85, 19, 31, 50, 74, 57]

sumStack :: Integer
sumStack = sumStackSub 0

sumStackSub :: Int -> Integer
sumStackSub counter = if (counter < (length numStack)) then
                            sumStackSub (counter + 1) + (numStack !! counter)
                         else
                            0   -- dummy value

どうもうありがとう!

ご挨拶!

4

1 に答える 1

3

コメントで指摘されているように、 mutate はできませんが、パラメーターとしての値を増やしcounterて呼び出すことができます。loopcounter

replaceBasedIdx    ::  String  ->  [String]  ->  String  ->  String
replaceBasedIdx    findStr replaceStrList myText = replaceBasedIdxSub findStr replaceStrList myText 0

replaceBasedIdxSub  ::  String  ->  [String]  ->  String  -> Int -> String
replaceBasedIdxSub findStr replaceStrList myText counter = loop myText counter
  where
    loop [] _ = []
    loop myText c =
      let (prefix, rest) = splitAt n myText
      in
        if findStr == prefix                                -- found an occurrence?
        then (replaceStrList !! (counter+1)) ++ loop rest (counter+1)   -- yes: replace it

        else head myText : loop (tail myText) (counter+1)              -- no: keep looking
    n = length findStr

2番目の例は機能します。これを行うだけなのでcounter、呼び出された関数にインクリメントされた値を渡しますsumStackSub

于 2013-05-08T07:19:05.950 に答える