递归的一个例子

package main

import (
    "fmt"
)

func merge(a []string, b []string) (c []string) {
    for _, vi := range a {
        for _, vj := range b {
            c = append(c, vi+vj)
        }
    }
    return c
}

func Merge(i int, args ...[]string) (c []string) {
    fmt.Println(args)
    if i == 1 {
        return args[0]
    }
    if i == 2 {
        return merge(args[0], args[1])
    }
    args[i-2] = merge(args[i-1], args[i-2])
    args[i-1] = []string{""}
    return Merge(i-1, args...)
}

func main() {

    a := []string{"a", "b"}
    b := []string{"c", "d"}
    c := []string{"e", "f"}
    d := []string{"g", "h"}
    j := Merge(4, a, b, c, d)
    fmt.Println(j)
}


运行结果
[[a b] [c d] [e f] [g h]]
[[a b] [c d] [ge gf he hf] []]
[[a b] [gec ged gfc gfd hec hed hfc hfd] [] []]
[agec aged agfc agfd ahec ahed ahfc ahfd bgec bged bgfc bgfd bhec bhed bhfc bhfd]

你可能感兴趣的:(递归的一个例子)