使用模式匹配交换列表中的元素对 [英] Swap pairs of elements in a list using pattern matching

查看:64
本文介绍了使用模式匹配交换列表中的元素对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用模式匹配技术,以便递归交换列表中的每对元素.因此,[1, 2, 3, 4, 5]将返回[2, 1, 4, 3, 5].

I need to use pattern matching techniques, in order to recursively swap every pair of elements in a list. So, [1, 2, 3, 4, 5] would return [2, 1, 4, 3, 5].

我发现了两件事:

  • List.length:返回长度.这对于处理偶数/奇数列表很有帮助.
  • List.nth:返回列表中指定位置的值.
  • drop.(list, i):在删除第一个i元素之后返回值.
  • List.length: to return the length. Which is helpful to deal with even/odd lists.
  • List.nth: to return a value at the designated spot in the list.
  • drop.(list, i): to return the values after the first i elements are dropped.

使用这三件事,我可以弄清楚一些递归方法,但是我不了解模式匹配方面.下面是一些没有任何模式匹配的伪代码.

Using those three things, I can sort of figure out some recursion methods, but I don't understand the pattern matching aspect. Below is some pseudocode without any pattern matching.

function(list):
  var a = first on list
  var b = second on list

  // odd case
  if(b = null | "")
  {
    list = drop.(list, 1) @ a
  }

  // even case
  else if(b = "" & a = "")
  {
    list = list
  }

  // recursive case
  else
  {
    list = function(drop.(list, 2) @ b @ a)
  }

从本质上讲,这可以开始循环遍历列表,交换对,将它们放置(连接)在列表的末尾,然后递归地重复此过程,直到遍历所有对为止.

This could essentially, start looping through the list, swapping the pairs, placing(concatenating) them at the end of the list, and recursively repeating this until it has gone through all pairs.

推荐答案

函数List.lengthList.nthList.drop似乎可以解决您的任务,但这与使用模式匹配的目标适得其反.通过模式匹配,您正在使用的列表是声明为类似的代数数据类型

The functions List.length, List.nth and List.drop seem like they might solve your task, but this is counterproductive to your goal of using pattern matching. With pattern matching you're using that lists are algebraic data types declared sort of like

datatype 'a list = [] | :: of 'a * 'a list

会为空列表生成值构造函数[],并为ins 生成infix ::运算符,该运算符在现有列表的前面(该元素为空或以相同方式创建) ),还有 pattern构造器,用于识别您要处理的列表类型.

which produces value constructors [] for empty list and the infix :: operator for cons'ing an element in front of an existing list (that is either empty or created in the same way), but also pattern constructors for recognizing what kind of list you're dealing with.

示例:一个获取整数列表并将其求和的函数:

Example: A function that takes a list of integers and sums them:

fun sum [] = 0
  | sum (x::xs) = x + sum xs

示例:一个函数,该函数接受一个2元组列表,并返回一个类似的2元组列表,但是每个2元组中的元素都被交换了:

Example: A function that takes a list of 2-tuples and returns a similar list of 2-tuples but with the elements in each 2-tuple swapped:

fun swap [] = []
  | swap ((x,y)::xys) = (y,x)::swap xys

示例:一个函数,该函数接受大小为偶数的整数,并返回一个将它们成对加在一起的列表:

Example: A function that takes an even-sized list of integers and returns a list where they've been added together in pairs:

fun addpairs (x::y::xys) = x + y :: addpairs xys
  | addpairs [_x] = raise Fail "Odd-sized list"
  | addpairs [] = []

特别是,模式可以嵌套((x,y)::xys ~~包含至少一个元素的列表,其中第一个元素是2元组,其第一部分命名为x,第​​二部分命名为x::y::xys ~~"列表中至少包含两个元素的列表,其中第一个元素名为x,第​​二个元素名为y),并且顺序(从上到下匹配)可能很重要重叠(他们在上述任何一项中都没有).

In particular, patterns can be nested ((x,y)::xys ~~ "the list with at least one element where the first element is a 2-tuple with the first part of it named x and the second part named y" and x::y::xys ~~ "the list with at least two elements in it with the first element named x and the second element named y"), and order (matching from top to bottom) might matter if patterns overlap (which they don't in any of the above).

这篇关于使用模式匹配交换列表中的元素对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆