代码之家  ›  专栏  ›  技术社区  ›  Derek

在Haskell中,我如何递归地操作一个元组,并将一个字符预附加到元组中的第一个元素上?

  •  1
  • Derek  · 技术社区  · 16 年前

    此函数的类型为 function :: Num a => ([Char],a) -> ([Char],a)

    (".'*",0) '.' '*' 和更新 a

    function ([], a) = ([], a)
    function ((x:xs), a)
        | x == '.' = ('-':xs, a+200)
        | x =='*' = ('-':xs, a+400)
        | otherwise = function(xs, a) --how do I put the unchanged x before xs without processing it?
    

    Main> function ("./-", 0)
    ("-/-",200)
    

    Main> function ("-/-", 0)
    ("-/-",0)
    

    1 回复  |  直到 14 年前
        1
  •  9
  •   Artelius    16 年前

    你忘了 function :: Num a => ([Char],a) -> ([Char],a)

        | otherwise = function(xs, a)
    

        | otherwise = let (xss, b) = function(xs, a)
                                  in (x:xss, b)