Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion F#-列表上的递归_Recursion_F# - Fatal编程技术网

Recursion F#-列表上的递归

Recursion F#-列表上的递归,recursion,f#,Recursion,F#,输入:[x1;x2;…;xn] 输出([x1;x3;…;xn],[x2;x4;…;xn],[x3;x5;…;xn],…) 到目前为止,我得到的是: let rec split = function | x0 :: x1 :: xs -> (x0::xs) :: split(x1::xs) | _ -> [] 这将产生: Input: [1;2;3;4;5;6] Output: [[1; 3; 4; 5; 6]; [2; 4; 5; 6]; [3; 5; 6]; [4; 6]; [5

输入:[x1;x2;…;xn]

输出([x1;x3;…;xn],[x2;x4;…;xn],[x3;x5;…;xn],…)

到目前为止,我得到的是:

let rec split = function
| x0 :: x1 :: xs -> (x0::xs) :: split(x1::xs)
| _ -> []
这将产生:

Input: [1;2;3;4;5;6]
Output: [[1; 3; 4; 5; 6]; [2; 4; 5; 6]; [3; 5; 6]; [4; 6]; [5]]
我不太明白如何递归地构建列表的元组


有什么想法吗?

我不知道你为什么要这样做,如果这是某种练习/家庭作业,我不想给你答案,但我会给你一个提示,最好从做正确方向的事情开始

这段代码接受一个列表,递归以一种非常不必要的复杂方式返回其中三个的元组

let rec tripleList : List<'a> -> (List<'a> * List<'a> * List<'a>) = 
    function 
    | []    -> ([],[],[])
    | (x :: xs) -> 
        let (tail1,tail2,tail3) = tripleList xs
        (x :: tail1,x :: tail2,x :: tail3)

> tripleList [1..10];;
val it : List<int> * List<int> * List<int> =
  ([1; 2; 3; 4; 5; 6; 7; 8; 9; 10], [1; 2; 3; 4; 5; 6; 7; 8; 9; 10],
   [1; 2; 3; 4; 5; 6; 7; 8; 9; 10])
let rec tripleList:List*List)=
作用
| []    -> ([],[],[])
|(x::xs)->
let(tail1、tail2、tail3)=三重列表xs
(x::tail1,x::tail2,x::tail3)
>三联主义者[1..10];;
val it:列表*列表*列表=
([1; 2; 3; 4; 5; 6; 7; 8; 9; 10], [1; 2; 3; 4; 5; 6; 7; 8; 9; 10],
[1; 2; 3; 4; 5; 6; 7; 8; 9; 10])
您也不需要将整个过程作为单个函数来完成,您可以创建3个函数,1个构建元组的每个部分,然后创建第四个函数调用这3个函数来组合答案。这可能是一个更简单的起点

所以类似的事情是

let rec tripleList1 : List<'a> -> List<'a> = 
    function 
    | []    -> []
    | (x :: xs) -> 
        (x :: tripleList1 xs)

let rec tripleList2 : List<'a> -> List<'a> = 
    function 
    | []    -> []
    | (x :: xs) -> 
        (2 * x :: tripleList2 xs)

let rec tripleList3 : List<'a> -> List<'a> = 
    function 
    | []    -> []
    | (x :: xs) -> 
        (3 * x :: tripleList3 xs)


let tripleList : List<int> -> (List<int> * List<int> * List<int>) = 
    fun xs -> 
        (tripleList1 xs,tripleList2 xs,tripleList3 xs)
let rec tripleList1:List=
作用
| []    -> []
|(x::xs)->
(x::tripleList1 xs)
let rec tripleList2:List=
作用
| []    -> []
|(x::xs)->
(2*x::tripleList2 xs)
let rec tripleList3:List=
作用
| []    -> []
|(x::xs)->
(3*x::TripleList3xs)
让三重列表:列表->(列表*列表*列表)=
趣味xs->
(tripleList1 xs、tripleList2 xs、tripleList3 xs)

我更喜欢第一种方法,但两者都是完全有效的,并且区分了创建递归数据结构所需的递归函数是否需要澄清这个问题@格伦斯威尔,这根本不是个问题。更多的是一个陈述,几乎没有上下文,后面是一个问号。正如你自己指出的,这项任务毫无意义。如果你想让它有意义,你必须问那些分配给你的人。我们不能像你一样理解他们的想法。我不指出这项任务毫无意义。事实上,我相信是的(不过,从实际角度来看可能不是这样)。问题是如何递归地构建元组?我不认为这个问题需要阅读老师的想法,因为元组不是递归数据结构。它也不是动态数据类型,它有固定数量的元素。因此,这是没有意义的。