简体   繁体   English

F#:递归函数:测试元素是否是给定列表的成员

[英]F#: Recursive Functions: test whether an element is a member of a given list

I'm trying to figure out how to code this. 我正在试图弄清楚如何编码。

Implement in F# a function that tests whether an element is a member of a given list. 在F#中实现一个测试元素是否是给定列表成员的函数。 Here is the type I want. 这是我想要的类型。

val memberof : 'a * 'a list -> bool when 'a : equality val memberof:'a *'列表 - > bool'a:equality

Here are examples of the function in action. 以下是该功能的实例。

memberof 1 [1;2;3];; 成员1 [1; 2; 3] ;; error FS0003: This value is not a function and cannot be applied 错误FS0003:该值不是函数,无法应用

memberof (1,[1;2;3]);; memberof(1,[1; 2; 3]);; val it : bool = true val it:bool = true

memberof (1, []);; memberof(1,[]);; val it : bool = false val it:bool = false

memberof (1,[2;3;4]);; memberof(1,[2; 3; 4]);; val it : bool = false val it:bool = false

heres what i've put together... 继承人我把它放在一起......

let rec memberof l =
    match (l:float) with
    | a.Item(l) -> l -> bool + (memberof l)

or 要么

let rec memberof l =
    match (l:float list) with
    | [] -> false
    | (a:float)::b -> (a)=(memberof b)->bool
let rec memberof (l : float list) (item : float) : bool =
    match l with
    | hd::tl when hd = item -> true
    | hd::tl -> memberof tl item
    | [] -> false

or 要么

let rec memberof (l : float list) (item : float) : bool =
    match l with
    | hd::tl ->
        if hd = item then
            true
        else
            memberof tl item
    | [] -> false

or 要么

let rec memberof (l : float list) (item : float) : bool =
     match l with 
     | [] -> false 
     | hd :: tl -> 
         hd = item
         || memberof tl item

Test cases 测试用例

let test001 = memberof [1.0; 2.0; 3.0] 0.0    
printfn "test001: %A" test001   

let test002 = memberof [1.0; 2.0; 3.0] 1.0  
printfn "test002: %A" test002   

let test003 = memberof [1.0; 2.0; 3.0] 2.0  
printfn "test003: %A" test003 

let test004 = memberof [1.0; 2.0; 3.0] 3.0  
printfn "test004: %A" test004   

let test005 = memberof [] 0.0    
printfn "test005: %A" test005 

Which outputs 哪个输出

val test001 : bool = false
val test002 : bool = true
val test003 : bool = true
val test004 : bool = true
val test005 : bool = false

The problem with 这个问题

let rec memberof l =
    match (l:float list) with
    | [] -> false
    | (a:float)::b -> (a)=(memberof b)->bool

is that 就是它

| (a:float)::b -> (a)=(memberof b)->bool

is pulling the list apart correctly with 正确地将列表拉开

 (a:float)::b

however 然而

 (a)=(memberof b)->bool

is not right. 是不正确的。

With recursive functions over a list you want to pull off the head of the list and process the head. 使用列表上的递归函数,您需要拉出列表的头部并处理头部。 Then you want to call the function again, this time passing the tail of the list as the new list variable, eg 然后你想再次调用该函数,这次将列表的尾部作为新的列表变量传递,例如

memberof tl item

Since this is a predicate , we only need to stop once we reach a desired true or false. 由于这是一个谓词 ,我们只需要在达到所需的真或假时停止。 In this example when true is found the function can end, so no need to call memberof for the remainder of the list. 在此示例中,当找到true时,函数可以结束,因此不需要为列表的其余部分调用memberof

For the particular signature you requested 对于您要求的特定签名

val memberof : item:'a * list:'a list -> bool when 'a : equality val memberof:item:'a * list:'列表 - > bool'a:equality

let rec memberof ((item : 'a), (list : 'a list)) : bool when 'T : equality =
    match list with
    | hd::tl ->
        if hd = item then
            true
        else
            memberof (item, tl)
    | [] -> false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM