如何组合等序元素(功能编程)? [英] How to combine equal sequence elements (functional programming)?

查看:82
本文介绍了如何组合等序元素(功能编程)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,该函数采用序列< 1、2、2、3>,并返回具有等元素的序列,这些元素的分组方式与<<< 1、1> ;、< 2,2> ,< 3 >>.

I want to write a function that takes in a sequence <1,1,2,2,3> and returns the sequence with equal elements grouped like <<1,1>, <2,2>, <3>>.

我使用的是序列,而不是列表,但是其中一些功能是相似的.我正在考虑使用的某些功能是map,reduce,制表,filter,append等.

I'm using sequences, not lists, but some of the functions are similar. Some of the functions I am thinking of using are map, reduce, tabulate, filter, append etc..

Reduce接受一个关联函数,并返回该运算符减少"的序列.因此,将op + 0降低为<1,2,3> = 6.

Reduce takes in an associative function and returns the sequence that is "reduced" by that operator. So, reduce op+ 0 <1,2,3> = 6.

我的第一个想法是使用map将序列提升一个级别. 因此,< 1,1,2,2,3> =><< 1>,< 1>,< 2>,< 2>,< 3 >>.

My first thought was to use map to raise the sequence by one level. So, <1,1,2,2,3> => <<1>,<1>,<2>,<2>,<3>>.

然后,我正在考虑使用reduce,在该方法中,我创建了一个函数,该函数接受像(x,y)这样的成对元素.如果x == y,那么我返回否则,我什么也不做.但是...这并不完全有效,因为函数在两种情况下都必须返回相同类型的东西.

Then, I was thinking of using reduce, in which I create a function that takes pairs of elements like (x,y). If x == y, then I return else, I do nothing. But...this doesn't exactly work since the function has to return something of the same type in both cases.

有人可以给我一些正确使用方法的提示,例如我可以使用哪些更高阶的函数?我正在使用SML,但我并没有要求任何人给我一个完整的答案,因此(使用任何一种功能语言,无论如何,)任何高级技巧都将不胜感激

Can someone give me some tips on the right path to take, like what higher order functions I could possibly use? I'm using SML but I'm not asking for anyone to give me a full blown answer so any high-level tips would be appreciated (in any functional language honestly)

推荐答案

我猜您所引用的reduce函数与F#中的fold函数相同:

I guess that the reduce function you are referring to is the same as the fold function in F#:

val fold : ('State -> 'Value -> 'State) -> 'State -> 'Value list -> 'State

这将获取一个值列表,以及一个初始状态和一个在迭代列表值的过程中转换状态的函数.

This takes a list of values, together with an initial state and a function that transforms the state while iterating through the values of the list.

您可以一次完成自己想做的事情.您需要保持一些状态.假设您在1,1,2,2,3的中间(例如,在第二个2上).现在您需要:

You can do what you want in a single fold. There are a couple of things that you'll need to keep in the state. Imagine you are somewhere in the middle of 1,1,2,2,3 (say, on the second 2). Now you'll need:

  • 您当前正在收集的值-2
  • 包含当前收集值的值列表-即[2](序列中的第一个2)
  • 您先前收集的值列表-[ [1; 1] ].
  • The value that you are currently collecting - that is 2
  • A list of values containing the currently collected values - that is [2] (the first 2 from the sequence)
  • A list of lists of values you collected previously - that is [ [1; 1] ].

您将从初始状态-1, [], []开始(使用-1作为一些不会出现在您的输入中的值).然后,您需要编写根据当前值转换状态的函数.这需要处理几种情况:

You would start with an initial state -1, [], [] (using -1 as some value that won't appear in your input). Then you need to write the function that transforms the state based on a current value. This needs to handle a couple of situations:

  • 如果该值与您一直在收集的值不同,则需要将收集到的值列表添加到列表列表中(除非它为空)
  • 当值相同时,您需要将其添加到现在收集的值列表中并继续

希望这能为您提供足够的信息,以找出如何执行此操作,而无需实际透露完整的源代码!

Hopefully, this gives you enough information to figure out how to do this, without actually revealing the full source code!

这篇关于如何组合等序元素(功能编程)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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