1
0
Эх сурвалжийг харах

reverse the arguments of cons_end so it fit better with the revers call operator

Tamo 1 жил өмнө
parent
commit
64b86261ba

+ 1 - 1
book/src/list-functions-lists.md

@@ -34,7 +34,7 @@ fn cons<A>(x: A, xs: List<A>) -> List<A>
 Append an element to the end of a list.
 
 ```nbt
-fn cons_end<A>(xs: List<A>, x: A) -> List<A>
+fn cons_end<A>(x: A, xs: List<A>) -> List<A>
 ```
 
 ### `is_empty`

+ 2 - 2
examples/tests/lists.nbt

@@ -10,8 +10,8 @@ assert_eq(tail(xs), [2, 3])
 
 assert_eq(cons(0, xs), [0, 1, 2, 3])
 
-assert_eq(cons_end(xs, 4), [1, 2, 3, 4])
-assert_eq(cons_end([], 0), [0])
+assert_eq(cons_end(4, xs), [1, 2, 3, 4])
+assert_eq(cons_end(0, []), [0])
 
 assert(is_empty([]))
 assert(!is_empty(xs))

+ 2 - 2
numbat/modules/core/lists.nbt

@@ -15,7 +15,7 @@ fn tail<A>(xs: List<A>) -> List<A>
 fn cons<A>(x: A, xs: List<A>) -> List<A>
 
 @description("Append an element to the end of a list")
-fn cons_end<A>(xs: List<A>, x: A) -> List<A>
+fn cons_end<A>(x: A, xs: List<A>) -> List<A>
 
 @description("Check if a list is empty")
 fn is_empty<A>(xs: List<A>) -> Bool = xs == []
@@ -55,7 +55,7 @@ fn range(start: Scalar, end: Scalar) -> List<Scalar> =
 fn reverse<A>(xs: List<A>) -> List<A> =
   if is_empty(xs)
     then []
-    else cons_end(reverse(tail(xs)), head(xs))
+    else cons_end(head(xs), reverse(tail(xs)))
 
 @description("Generate a new list by applying a function to each element of the input list")
 fn map<A, B>(f: Fn[(A) -> B], xs: List<A>) -> List<B> =

+ 1 - 1
numbat/src/ffi/lists.rs

@@ -36,8 +36,8 @@ pub fn cons(mut args: Args) -> Result<Value> {
 }
 
 pub fn cons_end(mut args: Args) -> Result<Value> {
-    let mut list = list_arg!(args);
     let element = arg!(args);
+    let mut list = list_arg!(args);
     list.push_back(element);
 
     return_list!(list)