Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions utilities/list-methods.metta
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@


;; Fold a tuple from left to right
(: List.foldl (-> (-> $a $b $b) $b (List $a) $b))
;;( List.foldl (-> (-> $a $b $b) $b (List $a) $b))
(= (List.foldl $f $i Nil) $i)
(= (List.foldl $f $i (Cons $h $t)) (chain ($f $h $i) $i' (List.foldl $f $i' $t)))

;; Fold a tuple from right to left
(: List.foldr (-> (-> $a $b $b) $b (List $a) $b))
;;( List.foldr (-> (-> $a $b $b) $b (List $a) $b))
(= (List.foldr $f $i Nil) $i)
(= (List.foldr $f $i (Cons $h $t)) ($f $h (List.foldr $f $i $t)))

Expand All @@ -29,29 +29,29 @@
(= (List.tFoldr (Cons $x $xs) $f $t $acc) (let $new-acc (List.tFoldr $xs $f $t $acc) (let $tr ($t $x) ($f $tr $new-acc))))

;; Define List.sum
(: List.sum (-> (List Number) Number))
;;( List.sum (-> (List Number) Number))
(= (List.sum $xs) (List.foldr + 0 $xs))


(: List.append (-> $a (List $a) (List $a)))
;;( List.append (-> $a (List $a) (List $a)))
(= (List.append $val Nil) (Cons $val Nil))
(= (List.append $val (Cons $head $tail)) (Cons $head (List.append $val $tail)))

;; Get an element by index from a list
(: List.getByIdx (-> (List $a) Number $a))
;;( List.getByIdx (-> (List $a) Number $a))
(= (List.getByIdx Nil $idx) (Error Nil (Index out of range)))
(= (List.getByIdx (Cons $head $tail) $idx) (if (== $idx 0 ) $head (List.getByIdx $tail (- $idx 1))) )

;; Insert an element to a presumably sorted list, remains sorted.
(: List.insert (-> $a (List $a) (List $a)))
;;( List.insert (-> $a (List $a) (List $a)))
(= (List.insert $x Nil) (Cons $x Nil))
(= (List.insert $x (Cons $head $tail))
(if (< $x $head)
(Cons $x (Cons $head $tail))
(Cons $head (List.insert $x $tail))))

;; Sort a List in ascending order
;; (: List.sort (-> (List $a) (List $a))) ;; FIx Type system not working in peTTa
;; ;;( List.sort (-> (List $a) (List $a))) ;; FIx Type system not working in peTTa
(= (List.sort Nil) Nil)

;; Default to sort in asecnding order
Expand All @@ -67,17 +67,17 @@
)

;; helper function to find the length of the list
(: List.length (-> (List $a) Number))
;;( List.length (-> (List $a) Number))
(= (List.length Nil) 0)
(= (List.length (Cons $head $tail)) (+ 1 (List.length $tail)))

;; Map a function over a list
(: List.map (-> (-> $a $b) (List $a) (List $b)))
;;( List.map (-> (-> $a $b) (List $a) (List $b)))
(= (List.map $f Nil) Nil)
(= (List.map $f (Cons $x $xs)) (Cons ($f $x) (List.map $f $xs)))

;; Map a function that takes an additional arg
(: List.mapOverArg (-> (-> $a $b $a) $b $arg (List $a)))
;;( List.mapOverArg (-> (-> $a $b $a) $b $arg (List $a)))
(= (List.mapOverArg $f Nil $arg) Nil)
(= (List.mapOverArg $f (Cons $head $tail) $arg)
(let $new-head ($f $head $arg)
Expand All @@ -86,17 +86,17 @@
(Cons $new-head (List.mapOverArg $f $tail $arg)))))

;; Filter a list based on a predicate.
(: List.filter (-> (-> $a Bool) (List $a) (List $a)))
;;( List.filter (-> (-> $a Bool) (List $a) (List $a)))
(= (List.filter $p Nil) Nil)
(= (List.filter $p (Cons $x $xs)) (if ($p $x) (Cons $x (List.filter $p $xs)) (List.filter $p $xs)))

;; Convert a list to an expression.
(: List.listToExpr (-> (List $a) Expression))
;;( List.listToExpr (-> (List $a) Expression))
(= (List.listToExpr Nil) ())
(= (List.listToExpr (Cons $x $xs)) (let $t (List.listToExpr $xs) (cons-atom $x $t)))

;; defining List.max function with a comparator that compares non-numerical type values
(: List.max (-> (-> $a $a Bool) (List $a) $a))
;;( List.max (-> (-> $a $a Bool) (List $a) $a))
(= (List.max $comparator Nil) Nil)
(= (List.max $comparator (Cons $x $xs))
(if (== $xs Nil)
Expand All @@ -107,22 +107,22 @@
(List.max $comparator $xs)))))

;; Overloading the above List.max with the built in >= comparison operator for operation on List of numbers
(: List.max (-> (List $a) $a))
;;( List.max (-> (List $a) $a))
(= (List.max (Cons $x $xs)) (List.max >= (Cons $x $xs)))

;; Checks if an element is member of a list
(: List.contains (-> $a (List $a) Bool))
;;( List.contains (-> $a (List $a) Bool))
(= (List.contains $a Nil) False)
(= (List.contains $a (Cons $head $tail)) (if (== $a $head) True (List.contains $a $tail)))

; Replaces an element at a specific index with another element
(: List.replaceAt (-> (List $a) Number $a (List $a)))
;;( List.replaceAt (-> (List $a) Number $a (List $a)))
(= (List.replaceAt Nil $n $elem) Nil)
(= (List.replaceAt (Cons $head $tail) $n $elem)
(if (== $n 0) (Cons $elem $tail) (Cons $head (List.replaceAt $tail (- $n 1) $elem))))

;; List.prepend .. adds an element at the beginnig of a list
(: List.prepend (-> $a (List $a) (List $a)))
;;( List.prepend (-> $a (List $a) (List $a)))
(= (List.prepend $a $list) (Cons $a $list))

;; Find an element in a list
Expand All @@ -132,7 +132,7 @@
;; Returns:
;; The index of the first found element or
;; -1 If the element can't be found
(: List.index (-> (List $a) $a Number))
;;( List.index (-> (List $a) $a Number))
(= (List.index Nil $target) -1)
(= (List.index (Cons $x $xs) $target)
(if (== $x $target)
Expand All @@ -147,7 +147,7 @@
;; $list: The list to return from
;; Returns:
;; The first element of the list
(: List.head (-> (List $a) $a))
;;( List.head (-> (List $a) $a))
(= (List.head Nil) (Error Nil EmptyList))
(= (List.head (Cons $x $xs)) $x)

Expand All @@ -157,29 +157,29 @@
;; $list: The list to return from
;; Returns:
;; The rest of the list
(: List.tail (-> (List $a) (List $a)))
;;( List.tail (-> (List $a) (List $a)))
(= (List.tail Nil) Nil)
(= (List.tail (Cons $x $xs)) $xs)

;; Subtract list element wise
(: List.sub (-> (List Number) (List Number) (List Number)))
;;( List.sub (-> (List Number) (List Number) (List Number)))
(= (List.sub Nil Nil) Nil)
(= (List.sub Nil (Cons $y $ys)) (Error (Cons $y $ys) LenghtOfListNotEqual))
(= (List.sub (Cons $x $xs) Nil) (Error (Cons $x $xs) LenghtOfListsNotEqual))
(= (List.sub (Cons $x $xs) (Cons $y $ys))
(chain (List.sub $xs $ys) $res (if-error $res $res (Cons (- $x $y) $res))))

(: List.zip (-> (List $a) (List $b) (List ($a $b))))
;;( List.zip (-> (List $a) (List $b) (List ($a $b))))
(= (List.zip Nil Nil) Nil)
(= (List.zip Nil (Cons $y $ys)) Nil)
(= (List.zip (Cons $x $xs) Nil) Nil)
(= (List.zip (Cons $x $xs) (Cons $y $ys)) (Cons ($x $y) (List.zip $xs $ys)))

(: List.zipWith (-> (-> $a $b $c) (List $a) (List $b) (List $c)))
;;( List.zipWith (-> (-> $a $b $c) (List $a) (List $b) (List $c)))
(= (List.zipWith $f Nil Nil) Nil)
(= (List.zipWith $f Nil (Cons $y $ys)) Nil) (= (List.zipWith $f (Cons $x $xs) Nil) Nil) (= (List.zipWith $f (Cons $x $xs) (Cons $y $ys)) (Cons ($f $x $y) (List.zipWith $f $xs $ys)))

(: List.drop (-> Number (List $a) (List $a)))
;;( List.drop (-> Number (List $a) (List $a)))
(= (List.drop $n Nil) Nil)
(= (List.drop $n (Cons $x $xs))
(if (== $n 0)
Expand All @@ -188,15 +188,15 @@

;; Temporary flatten function that works only for list of
;; lists where the second list has only single elements.
(: List.flatten (-> (List (List $a)) (List $a)))
;;( List.flatten (-> (List (List $a)) (List $a)))
(= (List.flatten Nil) Nil)
(= (List.flatten (Cons $x $xs))
(case $x
(
((Cons $y Nil) (chain (List.flatten $xs) $res (if-error $res $res (Cons $y $res))))
((Cons $y $ys) (Error $x "Can't be flattened")))))

(: List.repeat (-> Number $a (List $a)))
;;( List.repeat (-> Number $a (List $a)))
(= (List.repeat $n $a)
(if (== $n 0)
Nil
Expand All @@ -208,7 +208,7 @@
;; $list2: Second list to concatenate.
;; Returns:
;; (List $a) - New list containing all elements of $list1 followed by $list2.
(: List.concat (-> (List $a) (List $a) (List $a)))
;;( List.concat (-> (List $a) (List $a) (List $a)))
(= (List.concat Nil $list2) $list2)
(= (List.concat (Cons $head $tail) $list2)
(Cons $head (List.concat $tail $list2)))
Expand All @@ -219,7 +219,7 @@
;; $idx: Non-negative index of element to remove (0-based).
;; Returns:
;; (List $a) - New list with element at $idx removed, or original list if $idx is invalid.
(: List.removeAtIdx (-> (List $a) Number (List $a)))
;;( List.removeAtIdx (-> (List $a) Number (List $a)))
(= (List.removeAtIdx Nil $idx) Nil)
(= (List.removeAtIdx (Cons $head $tail) $idx)
(if (< $idx 0) (Cons $head $tail)
Expand All @@ -232,14 +232,14 @@
;; $list: Input list to search in.
;; Returns:
;; Bool - True if $elem is found in $list, False otherwise.
(: List.isMember (-> $a (List $a) Bool))
;;( List.isMember (-> $a (List $a) Bool))
(= (List.isMember $elem Nil) False)
(= (List.isMember $elem (Cons $head $tail))
(if (== $elem $head) True
(List.isMember $elem $tail)))

;; List.partialSort -- sorts the top n values in a list and leaves the rest unsorted
(: List.partialSort (-> (-> $a $a Bool) (List $a) Number (List $a) (List $a)))
;;( List.partialSort (-> (-> $a $a Bool) (List $a) Number (List $a) (List $a)))
(= (List.partialSort $comparator (Cons $x $xs) $n $acc)
(let* (($max (List.max $comparator (Cons $x $xs)))
($unsortedList (List.delete $max (Cons $x $xs)))
Expand All @@ -250,11 +250,11 @@
(List.partialSort $comparator $unsortedList (- $n 1) $sortedList))))

;; Overloading the above partialSort for partial Sorting list of numbers -- decreasing order
(: List.partialSort (-> (List Number) Number (List Number) (List Number)))
;;( List.partialSort (-> (List Number) Number (List Number) (List Number)))
(= (List.partialSort $list $n $acc) (List.partialSort >= $list $n $acc))

;; append a list to a list
(: List.appendList (-> (List $a) (List $a) (List $a)))
;;( List.appendList (-> (List $a) (List $a) (List $a)))
(= (List.appendList $a (Cons $x $xs))
(if (== $xs Nil)
(Cons $x $a)
Expand Down
4 changes: 2 additions & 2 deletions utilities/nodeId.metta
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
;; $levelId: - index indicating the level (0 for root, 1 for first child, etc.).
;; Returns:
;; (Tree $a) - The subtree at the specified level, or Nil if out of bounds.
(: getLevelById (-> (Tree $a) Number (Tree $a)))
;;(: getLevelById (-> (Tree $a) Number (Tree $a)))
(= (getLevelById (mkTree (mkNode $r) $children) $levelId)
(if (== $levelId 0)
(mkTree (mkNode $r) $children)
Expand All @@ -34,7 +34,7 @@
;; $id: - A mkNodeId-wrapped tuple of indices (e.g., (mkNodeId (2 1))) representing the path.
;; Returns:
;; (Tree $a) - The subtree at the specified ID path, or an error if invalid.
(: getNodeById (-> (Tree $a) NodeId (Tree $a)))
;;(: getNodeById (-> (Tree $a) NodeId (Tree $a)))
(= (getNodeById $tree (mkNodeId ())) $tree)
(= (getNodeById Nil $id) Nil)
(= (getNodeById $tree (mkNodeId $id))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

!(register-module! ../../../metta-moses)

! (import! &self metta-moses:utilities:list-methods)
! (import! &self metta-moses:utilities:general-helpers)
! (import! &self metta-moses:representation:instance)
!(import! &self metta-moses:scoring:cscore)
!(import! &self ../list-methods)
!(import! &self ../general-helpers)
!(import! &self ../../representation/instance)
!(import! &self ../../scoring/cscore)


;; Test cases for List.foldr
Expand Down Expand Up @@ -53,8 +51,8 @@
!(assertEqual (List.map isEven (Cons 1 (Cons 2 (Cons 3 Nil)))) (Cons False (Cons True (Cons False Nil))))
!(assertEqual (List.map isOdd Nil) Nil)

! (assertEqual (List.mapOverArg $f Nil $arg) Nil)
! (assertEqual (List.mapOverArg + (Cons 1 Nil) 1) (Cons 2 Nil))
!(assertEqual (List.mapOverArg $f Nil $arg) Nil)
!(assertEqual (List.mapOverArg + (Cons 1 Nil) 1) (Cons 2 Nil))

;; Test cases for List.filter
!(assertEqual (List.filter isEven (Cons 1 (Cons 2 (Cons 3 Nil)))) (Cons 2 Nil))
Expand Down Expand Up @@ -146,7 +144,7 @@
!(assertEqual (List.isMember 4 (Cons 1 (Cons 2 (Cons 3 Nil)))) False)

;; List.partialSort -- for list of Scored instances
! (assertEqual (List.partialSort instance>=
!(assertEqual (List.partialSort instance>=
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore 1 2 3 4 15)))
(Cons (mkSInst (mkPair (mkInst (Cons 1 Nil)) (mkCscore 1 2 3 4 5)))
(Cons (mkSInst (mkPair (mkInst (Cons 1 Nil)) (mkCscore 1 2 3 4 0.5))) Nil))) 2 Nil)
Expand All @@ -155,7 +153,7 @@
(Cons (mkSInst (mkPair (mkInst (Cons 1 Nil)) (mkCscore 1 2 3 4 5)))
(Cons (mkSInst (mkPair (mkInst (Cons 1 Nil)) (mkCscore 1 2 3 4 0.5))) Nil))))

! (assertEqual (List.partialSort instance>=
!(assertEqual (List.partialSort instance>=
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 1 1.0 0.5 -3.5)))
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -1.0 2 1.0 0.5 -2.5)))
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -3.0 3 1.0 0.5 -4.5))) Nil)))2 Nil)
Expand All @@ -164,7 +162,7 @@
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 1 1.0 0.5 -3.5)))
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -3.0 3 1.0 0.5 -4.5))) Nil))))

! (assertEqual (List.partialSort instance>=
!(assertEqual (List.partialSort instance>=
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 3 1.0 0.5 -3.5)))
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 2 1.0 0.5 -3.5)))
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 1 1.0 0.5 -3.5))) Nil))) 2 Nil)
Expand All @@ -173,7 +171,7 @@
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 2 1.0 0.5 -3.5)))
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 3 1.0 0.5 -3.5))) Nil))))

! (assertEqual (List.partialSort instance>=
!(assertEqual (List.partialSort instance>=
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -3.0 2 1.0 0.5 -4.5)))
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -1.0 3 1.0 0.5 -2.5)))
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 1 1.0 0.5 -3.5))) Nil))) 2 Nil)
Expand All @@ -182,37 +180,37 @@
(Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -3.0 2 1.0 0.5 -4.5))) Nil))))

;; For list of numbers
! (assertEqual (List.partialSort (Cons 5 (Cons 1 (Cons 8 (Cons 3 Nil)))) 2 Nil)
!(assertEqual (List.partialSort (Cons 5 (Cons 1 (Cons 8 (Cons 3 Nil)))) 2 Nil)
(Cons 8 (Cons 5 (Cons 1 (Cons 3 Nil)))) )
! (assertEqual (List.partialSort (Cons -4 (Cons -3 (Cons -2 (Cons -1 Nil)))) 2 Nil)
!(assertEqual (List.partialSort (Cons -4 (Cons -3 (Cons -2 (Cons -1 Nil)))) 2 Nil)
(Cons -1 (Cons -2 (Cons -4 (Cons -3 Nil)))))
! (assertEqual (List.partialSort (Cons 5 (Cons 1 (Cons 8 (Cons 3 (Cons 1 (Cons 7 Nil)))))) 3 Nil)
!(assertEqual (List.partialSort (Cons 5 (Cons 1 (Cons 8 (Cons 3 (Cons 1 (Cons 7 Nil)))))) 3 Nil)
(Cons 8 (Cons 7 (Cons 5 (Cons 1 (Cons 3 (Cons 1 Nil)))))))

;; For List.takeN
! (assertEqual (List.takeN 2 (Cons 2 (Cons 1 (Cons 3 Nil)))) (Cons 2 (Cons 1 Nil)))
! (assertEqual (List.takeN 0 (Cons 2 (Cons 1 (Cons 3 Nil)))) Nil)
!(assertEqual (List.takeN 2 (Cons 2 (Cons 1 (Cons 3 Nil)))) (Cons 2 (Cons 1 Nil)))
!(assertEqual (List.takeN 0 (Cons 2 (Cons 1 (Cons 3 Nil)))) Nil)

;; List.takeNFrom test cases
! (assertEqual (List.takeNFrom 2 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil)))))))
!(assertEqual (List.takeNFrom 2 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil)))))))
(Cons 2 (Cons 3 (Cons 4 Nil))))
! (assertEqual (List.takeNFrom 0 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil)))))))
(Cons 0 (Cons 1 (Cons 2 Nil))))
! (assertEqual (List.takeNFrom 3 2 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))))
!(assertEqual (List.takeNFrom 0 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil)))))))
(Cons 0 (Cons 1 (Cons 2 Nil))))
!(assertEqual (List.takeNFrom 3 2 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))))
(Cons 3 (Cons 4 Nil)))
! (assertEqual (List.takeNFrom 4 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))))
!(assertEqual (List.takeNFrom 4 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))))
(Cons 4 Nil))
! (assertEqual (List.takeNFrom 10 2 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil)))))
!(assertEqual (List.takeNFrom 10 2 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil)))))
Nil)
! (assertEqual (List.takeNFrom 2 0 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil)))))
!(assertEqual (List.takeNFrom 2 0 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil)))))
Nil)
! (assertEqual (List.takeNFrom 0 2 Nil)
!(assertEqual (List.takeNFrom 0 2 Nil)
Nil)
! (assertEqual (List.takeNFrom 4 1 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))))
!(assertEqual (List.takeNFrom 4 1 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))))
(Cons 4 Nil))
! (assertEqual (List.takeNFrom 2 10 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil)))))
!(assertEqual (List.takeNFrom 2 10 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil)))))
(Cons 2 (Cons 3 Nil)))
! (assertEqual (List.takeNFrom 2 10 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))))
!(assertEqual (List.takeNFrom 2 10 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))))
(Cons 2 (Cons 3 (Cons 4 Nil))))

;; List.generate test cases
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
!(register-module! ../../../metta-moses)
!(import! &self ../general-helpers)

! (import! &self metta-moses:utilities:list-methods)
! (import! &self metta-moses:utilities:tree)
! (import! &self metta-moses:utilities:nodeId)
!(import! &self ../list-methods)
!(import! &self ../tree)
!(import! &self ../nodeId)

;;; Testcase getLevelById
!(assertEqual
Expand Down
Loading