如何组合额外的列视图,最大值为2

时间:2015-06-05 09:18:36

标签: haskell

Update2 :是否有可能为3 ^ 2,4 ^ 2,5 ^ 2,... 10 ^ 2的函数返回动态数字类型,到目前为止我必须硬编码这个,即使加上这个, 我感觉不是一个完整的组合

comb0 :: [(Int, Int, Int)]
comb0 = do
   a <- [0,1,2]
   b <- [0,1,2]
   return (a, b, 2 - max a b)

comb :: [(Int, Int, Int, Int, Int, Int, Int, Int, Int)]
comb = do
   a1 <- [0]
   a2 <- [0,1]
   a3 <- [0]
   a4 <- [0,1] 
   a5 <- [0,1]
   a6 <- [0]
   a7 <- [0]
   a8 <- [0]
   a9 <- [0]
   return (a1,a2,a3,a4,a5,a6,a7,a8,a9)

Update1 :我认为这是一种愚蠢的方式,但可能有完整的组合

假设输出为[0; 1; 0]输入列1 [0; 0; 1]输入列2 [0; 1; 0]

因为输入1和输入2的第一行是0,所以在输出中不要先关注0

然后2减[1; 0]变为[1; 2]然后

我们看到有两行,所以make for for循环

for i in 0..1 do

 for j in 0..2 do

组合变为[0; 0],[0; 1],[0; 2],[1; 0],[1; 1],[1; 2]

加回[1; 0]变为[1 + 0; 0 + 0],[1 + 0; 0 + 1],[1 + 0; 0 + 2],[1 + 1; 0 + 1] ......然后胶水然后回到表

如何在haskell中以简单的方式编写这个?

原始问题: 如果只返回(a,b),则输入对于逻辑表

是正确的

这个问题是使用值2,你可以说它是3值, 当我计算5值时,我的计算机计算速度慢了c# 等待很长时间,仍然没有完成,我期望增加价值 尽可能多的希望haskell可以更快地在这个

期望为此添加输出列, 我用((a,b),max(a,b))作为起点,然后加一到三 输出直到达到2并且递归运行并保存在F#中写入的列表中的每个组合的列,输出12个组合输出,但我觉得有一些 缺少,而不是完整的组合,期望使用haskell做同样的事情来看到这个的完整组合并保存到文本文件中,而且还计算它有多少组合

如果第一行0除外,0也保持输出0

comb :: [(Int, Int, Int)]
comb = do
   b <- [0,1,2]
   a <- [0,1,2]
   return (a,b, max a b)

comb2 :: [(Int, Int, Int)] -> [(Int, Int, Int)]
comb2 x = do
   c <- [0,1,2]
   return (fst x, fst snd x, c + snd snd x)

[1/1]编译Main(main.hs,main.o)

main.hs:67:19:
    Couldn't match expected type `Int'
                with actual type `(Int, Int) -> (Int, Int)'
    In the return type of a call of `max'
    Probable cause: `max' is applied to too few arguments
    In the expression: max (a, b)
    In the first argument of `return', namely `((a, b), max (a, b))'

comb :: [((Int, Int), Int)]
comb = do
   b <- [0,1,2]
   a <- [0,1,2]
   return ((a,b), max(a,b))

  mapM_ print comb

F#代码

let MakeColumn(input : List<int> , m1 : int , m2 : int, numberofvalue : int) : List<int>=
    let mutable zero1 = 0
    for i in 0..(m1-1) do
        for j in 0..(m2-1) do
            input.Add(zero1)
        zero1 <- zero1 + 1
        if zero1 >= numberofvalue then
            zero1 <- 0
    input

let mutable col1_3 : List<int> = new List<int>()
let mutable col2_3 : List<int> = new List<int>()
let mutable col3_3 : List<int> = new List<int>()
let mutable col1_2 : List<int> = new List<int>()
let mutable col2_2 : List<int> = new List<int>()
let mutable col3_2 : List<int> = new List<int>()
let Radix = 3
let mutable totalrows = Convert.ToInt32(Math.Pow(float 2, float Radix))
col1_3 <- MakeColumn(col1_3, totalrows, 1, 2)
col2_3 <- MakeColumn(col2_3, totalrows/2, 2, 2)
col3_3 <- MakeColumn(col3_3, totalrows/4, 4, 2)
col1_2 <- MakeColumn(col1_2, totalrows/2, 1, 2)
col2_2 <- MakeColumn(col2_2, totalrows/4, 2, 2)
let debugtotalrows = totalrows
let debugcol1_3 = col1_3
let debugcol2_3 = col2_3
let debugcol3_3 = col3_3
let debugcol1_2 = col1_2
let debugcol2_2 = col2_2
let debugcol3_2 = col3_2
let mutable col1_3a : List<int> = new List<int>()
let mutable col2_3a : List<int> = new List<int>()
let debugcol1_3a = MakeColumn(col1_3a, 9/3, 3, 3)
let debugcol2_3a = MakeColumn(col2_3a, 9, 1, 3)
for i in 0..(col1_3.Count-1) do
    Console.WriteLine(col1_3.[i].ToString()+","+col2_3.[i].ToString())


let MinimumCovering(col1 : List<int>, col2 : List<int>)=
    let OutputBits = new List<int>()
    for i in 0..(col1.Count-1) do
        OutputBits.Add(Math.Max(col1.[i], col2.[i]))
    OutputBits

let MinimumCoveringList(col1 : List<int>, col2 : List<int>)=
    let mutable OutputBits = []
    for i in 0..(col1.Count-1) do
        OutputBits <- Math.Max(col1.[i], col2.[i]) :: OutputBits
    let finaloutput = List.rev OutputBits
    finaloutput

let NumberOfNonMaximumForCovering(col1 : List<int>)=
    let mutable count = 0
    for i in 0..(col1.Count-1) do
        if col1.[i] <> 2 then
            count <- count + 1
    count

let rec AllCombinationOfBasicOutput(NumberOfNonMaximumValue : int, MinimumCoveringOutputParam : int list, numberofloops : int, maximumvalue : int, finaloutput : Dictionary<int list, int list> byref)=
    let mutable newoutputs = new Dictionary<int list, int list>()
    if numberofloops-1 >= 0 then
        newoutputs <- new Dictionary<int list, int list>()
    for i in 0..(NumberOfNonMaximumValue-1) do 
        let mutable newoutput = []
        for j in 0..(MinimumCoveringOutputParam.Length-1) do //change one row at each time
            if i = j then
                if MinimumCoveringOutputParam.[j] + 1 < maximumvalue then
                    newoutput <- MinimumCoveringOutputParam.[j] + 1 :: newoutput
                else
                    newoutput <- MinimumCoveringOutputParam.[j] :: newoutput
            else
                newoutput <- MinimumCoveringOutputParam.[j] :: newoutput
        let newoutput5 = List.rev newoutput
        if finaloutput.ContainsKey(newoutput5) = false then
            finaloutput.Add(newoutput5, newoutput5)            
        if numberofloops-1 >= 0 then
            finaloutput <- AllCombinationOfBasicOutput(NumberOfNonMaximumValue, newoutput5, numberofloops - 1, maximumvalue, &finaloutput)
    finaloutput

let MinimumCoveringOutput = MinimumCovering(debugcol1_3a, debugcol2_3a)
let MinimumCoveringOutputList = MinimumCoveringList(debugcol1_3a, debugcol2_3a)         
let NumberOfNonMaximum = NumberOfNonMaximumForCovering(MinimumCoveringOutput)
let mutable newoutputs3 = new Dictionary<int list, int list>()   
let comboutputs = AllCombinationOfBasicOutput(NumberOfNonMaximum, MinimumCoveringOutputList, NumberOfNonMaximum, 3, &newoutputs3)
let ChangeIntListToListInt(m : int list) : List<int> =
    let result = new List<int>()
    for i in 0..(m.Length-1) do
        result.Add(m.[i])
    result

for aa in comboutputs.Keys do
    let newaa = ChangeIntListToListInt(aa)
    WriteTableMultipleValuedLogic(debugcol1_3a, debugcol2_3a,newaa, "E:/martintonic.txt")
    System.IO.File.AppendAllText("E:/martintonic.txt", "*****\r\n")

期望比较下面的输出,看看下面是否是完整组合

0,0=1
0,1=1
0,2=2
1,0=1
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=2
0,1=1
0,2=2
1,0=1
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=2
0,1=2
0,2=2
1,0=1
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=2
0,1=1
0,2=2
1,0=2
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=2
0,1=2
0,2=2
1,0=2
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=1
0,1=2
0,2=2
1,0=1
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=1
0,1=2
0,2=2
1,0=2
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=1
0,1=1
0,2=2
1,0=2
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=0
0,1=2
0,2=2
1,0=1
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=0
0,1=2
0,2=2
1,0=2
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=0
0,1=1
0,2=2
1,0=1
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****
0,0=0
0,1=1
0,2=2
1,0=2
1,1=1
1,2=2
2,0=2
2,1=2
2,2=2
*****

2 个答案:

答案 0 :(得分:2)

使用max a b,而不是max (a,b)。前者传递两个参数,后者只传递一个 - 一对(a,b)

答案 1 :(得分:0)

正如你问我的那样 - 你可以通过以下方式进行comb2编译:

comb2 :: [(Int, Int, Int)] -> [(Int, Int, Int)]
comb2 xs = do
   (a,b,c) <- xs
   d <- [0,1,2]
   return (a, b, c+d)   

我不确切地知道您要对fstsnd内容做些什么,但它看起来有点像LISP car&amp; cdr - 但那些人宁愿headtail,而且显然只适用于列表 - 不适用于元组;)

顺便说一句:因为你似乎对F#有点了解 - 你会在类似的派系中做到这一点 - 那些mutable值真的是反惯用的F#;)

> let comb2 xs = seq {
-    for (a,b,c) in xs do
-    for d in 0..2 do
-    yield (a,b,c+d) };;

val comb2 : xs:seq<'a * 'b * int> -> seq<'a * 'b * int>

正如您所看到的,您可以使用do 表达式 来代替seq块。