Data structures 我应该如何修改队列类以允许用户在F#中创建未指定类型的空队列?
我在F#中创建了一个不可变的Data structures 我应该如何修改队列类以允许用户在F#中创建未指定类型的空队列?,data-structures,f#,Data Structures,F#,我在F#中创建了一个不可变的队列,如下所示: type Queue<'a>(f : 'a list, r : 'a list) = let check = function | [], r -> Queue(List.rev r, []) | f, r -> Queue(f, r) member this.hd = match f with | [] -> failwith "
队列
,如下所示:
type Queue<'a>(f : 'a list, r : 'a list) =
let check = function
| [], r -> Queue(List.rev r, [])
| f, r -> Queue(f, r)
member this.hd =
match f with
| [] -> failwith "empty"
| hd :: tl -> hd
member this.tl =
match f, r with
| [], _ -> failwith "empty"
| hd::f, r -> check(f, r)
member this.add(x) = check(f, x::r)
static member empty : Queue<'a> = Queue([], [])
C:\Documents and Settings\juliet\Local Settings\Temp\stdin(5,5):错误FS0030:
值限制。已推断值“test”具有泛型类型
val测试:队列
如何修改我的
队列
类以允许用户创建空队列?您需要使用GeneralizableValueAttribute,一个la:
type Queue<'a>(f : 'a list, r : 'a list) = // '
let check = function
| [], r -> Queue(List.rev r, [])
| f, r -> Queue(f, r)
member this.hd =
match f with
| [] -> failwith "empty"
| hd :: tl -> hd
member this.tl =
match f, r with
| [], _ -> failwith "empty"
| hd::f, r -> check(f, r)
member this.add(x) = check(f, x::r)
module Queue =
[<GeneralizableValue>]
let empty<'T> : Queue<'T> = Queue<'T>([], []) // '
let test = Queue.empty
let x = test.add(1) // x is Queue<int>
let y = test.add("two") // y is Queue<string>
类型队列(List.rev r,[]
|f,r->队列(f,r)
各位议员,这是房屋署=
匹配
|[]->故障带“空”
|hd::tl->hd
成员:this.tl=
将f,r与
|[],[带“空”的故障
|hd::f,r->检查(f,r)
成员this.add(x)=检查(f,x::r)
模块队列=
[]
let empty=Queuepojh这应该是一个CW:)下选投票人:请提供一个你对这个问题下选的原因。@Princess:下选是匿名的。为什么你需要知道是谁做的?我在尝试修改标题后给了+1。我可以发誓我写的正是这个,但看起来你写得更好。非常感谢:)
> let test = Set.empty;;
val test : Set<'a>
type Queue<'a>(f : 'a list, r : 'a list) = // '
let check = function
| [], r -> Queue(List.rev r, [])
| f, r -> Queue(f, r)
member this.hd =
match f with
| [] -> failwith "empty"
| hd :: tl -> hd
member this.tl =
match f, r with
| [], _ -> failwith "empty"
| hd::f, r -> check(f, r)
member this.add(x) = check(f, x::r)
module Queue =
[<GeneralizableValue>]
let empty<'T> : Queue<'T> = Queue<'T>([], []) // '
let test = Queue.empty
let x = test.add(1) // x is Queue<int>
let y = test.add("two") // y is Queue<string>