Implement a queue type in F# -
i'm trying implement queue in f# far have think it's acting more stack:
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 empty" |que(x, xs) -> x;; let rec deque = function |nl -> failwith "queue empty" |que(x, xs) -> xs;; let rec build = function | [] -> nl | x::xs -> enque x (build xs);;
the operations working fine except enque, want make adds new element of queue instead of front.
currently putting @ front ; if want enqueue @ end of queue have progress way end , put value :
let rec enque m = function nl -> que (m, nl) | que (x, xs) -> que (x, enque m xs) // note : not tail-rec
Comments
Post a Comment