简体   繁体   中英

Using “cons” without the “::” operator ocaml

Is there a way to construct lists in OCaml without using the :: operator?

For example, I know that normally elements would be concatenated as follows:

1::[2; 3; 4]

which produces [1; 2; 3; 4] [1; 2; 3; 4] [1; 2; 3; 4] .

What I'm wondering is if it's possible to implement a method that takes

cons(1 cons(2 cons(3 cons (4 nil))))

and outputs the same result, as shown in the cons Wikipedia page .

Thank you.

I think that you are looking for List.cons . Which allows you to do

# List.(cons 1 (cons 2 (cons 3 (cons 4 []))));;
- : int list = [1; 2; 3; 4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM