How can I implement a tail-recursive list append?

后端 未结 3 793
无人及你
无人及你 2020-12-06 02:52

A simple append function like this (in F#):

let rec app s t =
   match s with
      | [] -> t
      | (x::ss) -> x :: (app ss t)

will

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 03:04

    From a quick glance at the F# sources, it seems the tail is internally mutable. A simple solution would be to reverse the first list before consing its elements to the second list. That, along with reversing the list, are trivial to implement tail recursively.

提交回复
热议问题