4
我試圖實現在F#隊列中的隊列類型到目前爲止,這是我,但我認爲它的作用更像是一個堆棧:實現在F#
type 'a queue = NL| Que of 'a * 'a queue;;
let enque m = function
|NL -> Que(m, NL)
|Que(x, xs) -> Que(m, Que(x, xs));;
let rec peek = function
|NL -> failwith "queue is empty"
|Que(x, xs) -> x;;
let rec deque = function
|NL -> failwith "queue is empty"
|Que(x, xs) -> xs;;
let rec build = function
| [] -> NL
| x::xs -> enque x (build xs);;
的操作是,除了做工精細enque,我想這樣做,它會在隊列的後面添加一個新元素,而不是前面。