MeTTa Task
unknown
lisp
a year ago
2.2 kB
6
Indexable
; Task: A function that takes a grandchild and a result set and updates the guard set accordingly. ; Components ;;;;;;;;; Component 1 ;;;;;;;;;;;; ; find-object: accepts a list and and an instance, iterates through it, and returns True if object is available, else False ; (: find-object (-> List TreeNode Bool)) (= (find-object $list $instance) ( case $list ( (Nil False) ((Cons $x $xs) ( if (== $x $instance) True (find-object $xs $instance) )) ($_ ERROR) ) ) ) ! (find-object (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))) 7) ! (find-object (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))) 4) ;;;;;; Component 2: Set Difference Calculator Returns $list1 - $list2 (A-B in mathematical terms) ;;;;;;;;;;;;;;; ; LOGIC: Iterate over objects of $list1, if they don't exist in $list2 add them to our list ; (:set-difference (-> List List List)) (= (set-difference $list1 $list2) ( case $list1 ( (Nil Nil) ; Base Case: Reaches the end of the list ((Cons $x $xs) ( ; Destructure the list, one value to $x the rest of the list to $xs if (find-object $list2 $x) ; if condition (set-difference $xs $list2) ; pass if object found in @list2 (Cons $x (set-difference $xs $list2)) ; add to our result and continue if not found in $list2 )) ) ) ) ! (set-difference (Cons 1 (Cons 2 (Cons 7 (Cons 4 Nil)))) (Cons 1 (Cons 2 (Cons 3 (Cons 5 Nil))))) (TreeNode $nodeValue $left $right $guardSet $children) ; LOGIC: destructure grandChild and get the $guardSet, then calculate its set difference with result set ; ( : compute-grandchild-guardset (-> TreeNode List)) ( = (compute-grandchild-guardset $grandChild $resultset) ( case $grandChild ( ((TreeNode $nodeValue $left $right $guardSet $children) (TreeNode $nodeValue $left $right (set-difference $guardSet $resultset) $children)) ($_ ERROR) ) ) ) ; ! (compute-grandchild-guardset )
Editor is loading...
Leave a Comment