# How to implement list\_copy?

**URL:** <https://discourse.ats-lang.org/t/how-to-implement-list-copy/708>\
**Category:** Google Group\
**Created:** [August 16, 2013, 5:39pm UTC](https://discourse.ats-lang.org/t/how-to-implement-list-copy/708 "2013-08-16T17:39:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [August 16, 2013, 5:39pm UTC](https://discourse.ats-lang.org/t/how-to-implement-list-copy/708/1 "2013-08-16T17:39:45Z")

</div>

The function list\_copy makes a copy of a given list. So its type  
can be given as follows:

extern  
fun{a:t0p}  
list\_copy {n:int} (xs: list (a, n)): list (a, n)

which means that the function returns a list of length n when called  
on a list of the same length.

How can it be implemented? Before I present my answer, I would like to  
see if anyone else wants to give it a try 🙂

Please don’t be afraid of getting it wrong because getting-it-wrong is  
ususally  
the start of learning something new.

Cheers!

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [August 19, 2013, 3:27pm UTC](https://discourse.ats-lang.org/t/how-to-implement-list-copy/708/2 "2013-08-19T15:27:57Z")

</div>

that’s right the tail recursive ways involves one more reverse step…  
That’s why people concerned with performance could have a look to the more  
advanced solution 😉

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [August 19, 2013, 6:52pm UTC](https://discourse.ats-lang.org/t/how-to-implement-list-copy/708/3 "2013-08-19T18:52:08Z")

</div>

Here is a one-traversal implementation of list\_copy in ATS2:

extern  
fun{a:t0p}  
list\_copy3  
{n:int} (xs: list (a, n)): list\_vt (a, n)  
// end of [list\_copy3]

implement{a}  
list\_copy3 (xs) = let  
//  
fun loop{n:int}  
(  
xs: list (a, n), res: &ptr? \>\> list\_vt (a, n)  
) : void = let  
in  
case xs of  
| list\_cons (x, xs1) =\>  
{  
val () =  
res := list\_vt\_cons{a}{0}(x, _)  
val+list\_vt\_cons (_, res1) = res  
val () = loop (xs1, res1)  
prval () = fold@ (res)  
}  
| list\_nil ((_void_)) =\> res := list\_vt\_nil ()  
end // end of [list\_copy3]  
//  
var res: ptr  
val () = loop (xs, res)  
//  
in  
res  
end // end of [list\_copy3]

The syntax for this in ATS2 is slightly different from ATS. For comparing  
several different implementations  
of list\_copy, please follow the link below:

> <https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/list_copy.dats>

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [August 19, 2013, 3:18pm UTC](https://discourse.ats-lang.org/t/how-to-implement-list-copy/708/4 "2013-08-19T15:18:47Z")

</div>

That is right. However, this approach is a bit too involved. Only good for  
experts 🙂

Usually, one implements list\_copy as follows:

implement{a}  
list\_copy (xs) =  
(  
case+ xs of  
| list\_cons  
(x, xs) =\> list\_cons (x, list\_copy (xs))  
| list\_nil () =\> list\_nil ()  
)

This is a non-tail-recursive version. If one only uses it to copy short  
lists, then it is fine.  
When handling a long list (say, containing 1 million elements), this  
function is likely to  
overflow the call stack.

Suppose list\_rcopy copies a list in the reverse order; that is, the return  
result is the reverse  
of the given list. Often, list\_rcopy is called list-reverse function, which  
can be readily implemented  
tail-recursively:

implement{a}  
list\_rcopy (xs) = let  
//  
fun loop{n1,n2:int}  
(  
xs: list (a, n1), res: list (a, n2)  
) : list (a, n1+n2) =  
case xs of  
| list\_cons  
(x, xs) =\> loop (xs, list\_cons (x, res))  
| list\_nil () =\> res  
//  
in  
loop (xs, list\_nil)  
end // end of [list\_rcopy]

Now, we can implement list\_copy as follows

implement{a}  
list\_copy (xs) = list\_rcopy (list\_rcopy (xs))

This is a tail-recursive implementation of list\_copy. However, one may  
dislike it because it does two list-traversals.  
Actually, the list-merge function (for implementing merge-sort) in ocaml’s  
library is done this way so as to make sure  
that list-merge is defined tail-recursively.

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [August 19, 2013, 7:36am UTC](https://discourse.ats-lang.org/t/how-to-implement-list-copy/708/5 "2013-08-19T07:36:05Z")

</div>

hello,  
sorry I don’t have much time to work on this but I suppose it would be  
similar to that single pass list filter illustrated here :

[http://bluishcoder.co.nz/2011/12/16/pattern-matching-against-linear-objects-in-ats.html#single\_pass\_tail\_recursive\_filtering](http://bluishcoder.co.nz/2011/12/16/pattern-matching-against-linear-objects-in-ats.html#single_pass_tail_recursive_filtering)

😉
