将一个整数附加到Ocaml中的列表

时间:2021-10-08 18:16:08

How can I implement this function the hard way without the @ operator ?

如果没有@操作符,我如何实现这个函数?

let rec append l i =

    (* For example, if l is a list [1;2] and i is an integer 3
              append [1;2] 3 = [1;2;3]*)
;;

3 个解决方案

#1


13  

Without using an existing append function, or even any existing function, only pattern matching:

不使用现有的追加函数,甚至不使用任何现有函数,只使用模式匹配:

let rec insert_at_end l i =
  match l with
    [] -> [i]
  | h :: t -> h :: (insert_at_end t i)

# insert_at_end [1;2] 3  ;;
- : int list = [1; 2; 3]

Also note that most of OCaml's standard library is written in OCaml. You can get the source code for the function that you want, or in this case, almost the function that you want, by reading the source package. In this case:

还要注意,OCaml的大多数标准库都是用OCaml编写的。您可以通过读取源包获得所需的函数的源代码,在本例中,几乎是所需的函数。在这种情况下:

file ocaml-3.11.1/stdlib/pervasives.ml

文件ocaml-3.11.1 / stdlib / pervasives.ml

(* List operations -- more in module List *)

let rec (@) l1 l2 =
  match l1 with
    [] -> l2
  | hd :: tl -> hd :: (tl @ l2)

#2


11  

The easy answer is:

最简单的答案是:

let append l i = l @ [i]

List-append is provided as the infix function @ in ocaml, so there is no need to roll your own. It is not tail recursive in the default ocaml distribution, but you can use extlib and begin your source file with:

List-append作为infix函数在ocaml中提供,所以不需要自己滚动。在默认的ocaml分布中,它不是尾递归,但是您可以使用extlib并开始使用以下内容:

open Extlib
open ExtList

And that provides a tail-recursive @ implementation. You can also use batteries or Jane Street Core for a tail-recursive append.

这提供了尾部递归@实现。您也可以使用电池或简街核心为尾部递归的附加。

#3


2  

Here's one tail-recursive implementation, if you want to do everything by hand (and that's not so difficult).

这里有一个尾部递归实现,如果您想手工完成所有事情(这并不困难)。

First, a function that reverses a list:

首先,反转列表的函数:

let mirror l =
    let rec aux accu = function
    | [] -> accu
    | h::t -> aux (h::accu) t
in aux [] l

The use of an auxiliary function is quit common to achieve tail-recursivity.

为了实现尾部递归性,通常不使用辅助函数。

Now the actual "append" function:

现在实际的“追加”函数:

let append l i = mirror (i::(mirror l))

#1


13  

Without using an existing append function, or even any existing function, only pattern matching:

不使用现有的追加函数,甚至不使用任何现有函数,只使用模式匹配:

let rec insert_at_end l i =
  match l with
    [] -> [i]
  | h :: t -> h :: (insert_at_end t i)

# insert_at_end [1;2] 3  ;;
- : int list = [1; 2; 3]

Also note that most of OCaml's standard library is written in OCaml. You can get the source code for the function that you want, or in this case, almost the function that you want, by reading the source package. In this case:

还要注意,OCaml的大多数标准库都是用OCaml编写的。您可以通过读取源包获得所需的函数的源代码,在本例中,几乎是所需的函数。在这种情况下:

file ocaml-3.11.1/stdlib/pervasives.ml

文件ocaml-3.11.1 / stdlib / pervasives.ml

(* List operations -- more in module List *)

let rec (@) l1 l2 =
  match l1 with
    [] -> l2
  | hd :: tl -> hd :: (tl @ l2)

#2


11  

The easy answer is:

最简单的答案是:

let append l i = l @ [i]

List-append is provided as the infix function @ in ocaml, so there is no need to roll your own. It is not tail recursive in the default ocaml distribution, but you can use extlib and begin your source file with:

List-append作为infix函数在ocaml中提供,所以不需要自己滚动。在默认的ocaml分布中,它不是尾递归,但是您可以使用extlib并开始使用以下内容:

open Extlib
open ExtList

And that provides a tail-recursive @ implementation. You can also use batteries or Jane Street Core for a tail-recursive append.

这提供了尾部递归@实现。您也可以使用电池或简街核心为尾部递归的附加。

#3


2  

Here's one tail-recursive implementation, if you want to do everything by hand (and that's not so difficult).

这里有一个尾部递归实现,如果您想手工完成所有事情(这并不困难)。

First, a function that reverses a list:

首先,反转列表的函数:

let mirror l =
    let rec aux accu = function
    | [] -> accu
    | h::t -> aux (h::accu) t
in aux [] l

The use of an auxiliary function is quit common to achieve tail-recursivity.

为了实现尾部递归性,通常不使用辅助函数。

Now the actual "append" function:

现在实际的“追加”函数:

let append l i = mirror (i::(mirror l))