在sml中压缩2个以上的列表
我的功能中有一个尾巴问题。我想去掉尾巴,但我想不出一个办法。如果有人能帮我找到解决办法,我会很高兴的在sml中压缩2个以上的列表,sml,Sml,我的功能中有一个尾巴问题。我想去掉尾巴,但我想不出一个办法。如果有人能帮我找到解决办法,我会很高兴的 fun Rollists (x::nil) = [x] | Rollists (xs) =(map hd xs)::Rollists( map tl xs); 此函数用于将给定列表中的元素成对地从每个列表中输出。ListPair.zip的更大版本基于第一个列表从列表中生成成对: fun generateTuples (lol) = let (* Treat the Li
fun Rollists (x::nil) = [x]
| Rollists (xs) =(map hd xs)::Rollists( map tl xs);
此函数用于将给定列表中的元素成对地从每个列表中输出。
ListPair.zip的更大版本基于第一个列表从列表中生成成对:
fun generateTuples (lol) =
let
(* Treat the ListOfLists as a database
table, with the first
list being a key value column *)
val keys = hd(lol)
(* and the remaining columns being
additional fields in its tuple *)
val records = tl(lol)
(* Pairs the key with each column
in its record *)
fun formPairs (aKey,listOfRecords) =
if null listOfRecords
then []
else [aKey,hd(hd listOfRecords)]::
(formPairs(aKey,tl(listOfRecords)))
(* Pops a row's data fields from the record *)
fun chopLists (listOfRecords)=
if null listOfRecords
then []
else tl(hd(listOfRecords))::
(chopLists(tl(listOfRecords)))
in
(* Pass the first key value to formPairs
along with all the records. Then pop
the first row off the database and call
generateTuples on the remain *)
if null keys
then []
else generateTuples(tl(keys)::(chopLists(records)))
@ formPairs(hd(keys),records)
end
例如:
val list1 = [0,1,2,3,4]
val list2 = [55,66,77,88,99]
val list3 = [10,11,12,13,14]
val list4 = [555,666,777,888,999]
val lols = [list1,list2,list3,list4]
- generateTuples(lols);
val it =
[[4,99],[4,14],[4,999],[3,88],[3,13],[3,888],[2,77],[2,12],[2,777],[1,66],
[1,11],[1,666],...] : int list list
你有什么问题?这有点难以解决。另外,在第二个例子中,您缺少了一个openparen。当然,我正在尝试实现Quine-McCluskey算法,所以我想把所有包含原始布尔函数的素数蕴涵式相加,然后取最便宜的一个。所以最难的部分是这个“加法”部分,而不是我想要的输出,但这项工作已经完成了。我需要generateTuples来输出子列表,子列表的大小与list of list的参数头的大小相同,也就是说,连接以相同元素开头的generateTuples的所有子列表,并确保此元素只在新子列表中输入一次。我将尝试修复@ben Rudger现在为尾部调用设计反转并过滤重复项,generateTuples(lol)的输出是否为[[0,55],[0,66],[0,77],[0,88],[0,99],…[4888][4,999]]]?