F#:递归函数:测试元素是否为给定列表的成员 [英] F#: Recursive Functions: test whether an element is a member of a given list

查看:60
本文介绍了F#:递归函数:测试元素是否为给定列表的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图弄清楚该如何编码.

I'm trying to figure out how to code this.

在F#中实现一个功能,该功能测试元素是否为给定列表的成员.这是我想要的类型.

Implement in F# a function that tests whether an element is a member of a given list. Here is the type I want.

val memberof:"a *"列表->当"a:等于"时出现布尔值

val memberof : ’a * ’a list -> bool when ’a : equality

以下是该功能的示例.

成员1 [1; 2; 3] ;; 错误FS0003:此值不是函数,无法应用

memberof 1 [1;2;3];; error FS0003: This value is not a function and cannot be applied

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

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

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

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

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

memberof (1,[2;3;4]);; 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)

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

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

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

测试用例

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 

哪个输出

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

问题

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

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

使用正确地将列表拉开

 (a:float)::b

但是

 (a)=(memberof b)->bool

不对.

要使用列表上的递归函数,您要拉出列表的头并处理该头.然后您要再次调用该函数,这一次将列表的尾部作为新的列表变量传递,例如

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, e.g.

memberof tl item

由于这是谓词,因此我们只需在到达期望的是非.在此示例中,当找到true时,该函数可以结束,因此无需为列表的其余部分调用memberof.

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.

对于您要求的特定签名

val memberof:item:'a * list:'a list->当'a:相等时bool

val memberof : item:'a * list:'a list -> bool when '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

这篇关于F#:递归函数:测试元素是否为给定列表的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆