【问题标题】:SML - Recursive function for list stringSML - 列表字符串的递归函数
【发布时间】:2015-08-16 06:09:24
【问题描述】:

我需要你的帮助! 我正在尝试创建一个函数,该函数将两个类型的元素作为输入

    (string*string*string) list 

    (string*string) list

并返回一个类型的元素

    (string*string) list 

以特定方式进行操作。 我需要类似的东西:

    returnString(([("s0","l0","s1"),("s1","l1","s0")]),([("s0","phi1"),("l0","chi1"),("l1","chi2"),("s1","phi2")])); 

接受这些输入的函数应该返回我:

    val it = [(("s0l0s1","chi1"),("s1l1s0","chi2"))]

应该是: 如果第一个输入元素的第二个字符串

    (string*string*string) 

对应第二个输入元素的第一个字符串

    (string*string) 

然后我会将我需要的元素放在列表中,否则我会继续检查。

我尝试了很多方法......使用递归函数,使用地图函数......但我对这种语言很陌生,我找不到方法,因为 sml 不容易用循环处理。

如果您能帮助我,或者即使您有一些建议可以提出,我将不胜感激。

非常感谢大家!

【问题讨论】:

    标签: string list recursion sml


    【解决方案1】:

    如果代码中的解释有意义,请告诉我。

    fun example (xss, yss) =
      case (xss, yss) of
        (* If the 1st list is empty, then the result is an empty list. *)
        ([], _) => []
        (* If the 2nd list is empty, then the result is also an empty list. *)
      | (_, []) => []
        (* Otherwise, we decompose the tuples in the two lists *)
      | ((a, b, c) :: xs, (x, y) :: ys) =>
          (* verify that our condition holds *)
          if b = x then
            (* in which case we have a solution and recurse with the rest *)
            (a ^ b ^ c, y) :: example (xs, ys)
          else
            (* otherwise, we recurse with the first list intact, but skip the *)
            (* current element in the second list. *)
            example (xss, ys)
    

    另外,请查看 this answer of mine,了解如何在标准 ML 中调用函数。

    【讨论】:

    • 哦,它完全符合我的要求:D 并且解释非常详尽!非常感谢..我会检查您对 SML 中功能的解释以更好地理解它
    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 2011-12-22
    • 2015-06-19
    • 1970-01-01
    • 2021-07-20
    • 2021-12-16
    • 1970-01-01
    • 2011-10-24
    相关资源
    最近更新 更多