删除F#列表中的第三个元素 [英] Delete third element in F# list

查看:121
本文介绍了删除F#列表中的第三个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数以返回减去第三个值的列表. 这是我当前的代码:

I am writing a function to return a list minus the third value. Here is my current code:

let listString = [ "1"; "2"; "3"; "4" ];;

let del3 (listA :'a)  =  [listA.Head; listA.Tail.Head] @ [listA.Tail.Tail.Tail];;

del3 listString

我得到了错误:

根据此程序点之前的信息查找不确定类型的对象.在此程序指向之前可能需要类型注释,以约束对象的类型.这样可以解决查找问题.

Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.

该如何解决该错误?

推荐答案

我认为基于模式匹配的简单方法可能会更好

I think a simpler approach based on a pattern match might be better

let del3 = function |a::b::c::d -> a::b::d | _ -> failwith "insufficient input"

这篇关于删除F#列表中的第三个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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