【问题标题】:Zipping on more than 2 lists in sml在 sml 中压缩超过 2 个列表
【发布时间】:2013-12-05 12:17:50
【问题描述】:

我的函数中有尾部问题。我想摆脱尾部,但我想不出办法。如果有人可以帮助我找到出路,我会很高兴。

fun Rollists (x::nil) = [x]
  | Rollists (xs) =(map hd xs)::Rollists( map tl xs); 

这个函数,应该将给定列表列表中的元素作为每个列表的对输出,ListPair.zip的更大版本

【问题讨论】:

  • 你有什么问题?否则很难解决。此外,您在第二种情况下缺少一个 openparen。
  • 当然,我正在尝试实现 Quine McCluskey 算法,所以我想将所有涵盖原始布尔函数的质蕴涵项相加,然后取最便宜的那个。所以困难的部分是这个“加法” " 部分

标签: sml


【解决方案1】:

根据第一个列表从列表列表中生成对:

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

【讨论】:

  • 不完全是我想要的输出,但工作已经完成了。我需要 generateTuples 来输出与列表列表参数头部大小相同的子列表,即连接所有子列表以相同元素开头的 generateTuples 并确保该元素仅在新子列表中输入一次。我现在将尝试修复该问题@ben rudgers
  • 忽略尾调用的反转和重复过滤,generateTuples(lol) 的输出会是 [[[0,55],[0,66],[0,77],[ 0,88],[0,99]],...[4,888][4,999]]]?
猜你喜欢
  • 2015-08-24
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 2022-01-08
  • 2013-10-26
  • 1970-01-01
  • 2018-02-11
  • 1970-01-01
相关资源
最近更新 更多