两表跨产品产品、两表跨

由网友(沵给的承诺╮已过期)分享简介:与扩展功能的列表模块瞎搞。(我花了相当长的一段发展mapfold - 这螺纹状褶皱蓄能器,而是使用它作为一个参数创建像地图新的价值 - 然后发现,这就是 List.scan_left 一样)的有关生成测试数据,我需要做的两个列表交叉的产品,这就是我想出了:///执行两个列表交叉的产品,回报元组让双重交叉L1 L2...

与扩展功能的列表模块瞎搞。 (我花了相当长的一段发展mapfold - 这螺纹状褶皱蓄能器,而是使用它作为一个参数创建像地图新的价值 - 然后发现,这就是 List.scan_left 一样)的

有关生成测试数据,我需要做的两个列表交叉的产品,这就是我想出了:

  ///执行两个列表交叉的产品,回报元组
让双重交叉L1 L2 =
    让产品LST V2 = List.map(乐趣V1  - >(V1,V2))LST
    List.map_concat(产品L1)12
 

这是什么好,或者是有已经有一些更好的方法来做到这一点?

这一个同样的问题:

  ///执行三个列表交叉的产品,回报元组
让crossproduct3 L1 L2 L3 =
    让tuplelist =双重交叉​​L1 L2 //不知道这是最好的方式...
    让产品3 LST2 V3 = List.map(FUN(V1,V2) - >(V1,V2,V3))LST2
    List.map_concat(产品3 tuplelist)13
 
商品促销分析与总结最常用的4张表

解决方案

另一种选择是使用F#序列的前pressions和这样写:

 让双重交叉L1 L2 =
  序列{为EL1的L1做
          为EL2在12做
            产量EL1,EL2} ;;
 

(实际上,它几乎是同样的事情,你写的是什么,因为为..在..做'序列中前pression可以被看作是map_concat)。这适用于(懒惰)序列,但如果你想用列表来上班,你只是包装内的code [...],而不是内部的序列{...}。

Messing around with 'extension functions' for the List module. (I spent quite a while developing 'mapfold' - which threads an accumulator like fold, but uses it as a parameter to create new values like map - then discovered that that is what List.scan_left does)

For generation of test data, I needed to do a cross product of two lists, This is what I came up with:

///Perform cross product of two lists, return tuple
let crossproduct l1 l2 =
    let product lst v2 = List.map (fun v1 -> (v1, v2)) lst
    List.map_concat (product l1) l2

Is this any good, or is there already some better way to do this?

Same question for this one:

///Perform cross product of three lists, return tuple
let crossproduct3 l1 l2 l3 =
    let tuplelist = crossproduct l1 l2 //not sure this is the best way...
    let product3 lst2 v3 = List.map (fun (v1, v2) -> (v1, v2, v3)) lst2
    List.map_concat (product3 tuplelist) l3

解决方案

another option is to use F# "sequence expressions" and write something like this:

let crossproduct l1 l2 =
  seq { for el1 in l1 do
          for el2 in l2 do
            yield el1, el2 };;

(actually, it is almost the same thing as what you wrote, because 'for .. in .. do' in sequence expression can be viewed as map_concat). This works with (lazy) sequences, but if you want to work with lists, you'd just wrap the code inside [ ... ] rather than inside seq { ... }.

阅读全文

相关推荐

最新文章