Created
July 8, 2025 04:13
-
-
Save mrryanjohnston/680deaee87533dfedc74b461eef5a358 to your computer and use it in GitHub Desktop.
pairsSumToZero
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Based on https://markushimmel.de/blog/my-first-verified-imperative-program/ | |
(defrule sum-is-0 | |
(list $? ?first $? ?second $?) | |
(test (= 0 (+ ?first ?second))) | |
=> | |
(println TRUE)) | |
(defrule sum-is-not-0 | |
(not (and | |
(list $? ?first $? ?second $?) | |
(test (= 0 (+ ?first ?second))))) | |
=> | |
(println FALSE)) | |
(assert (list 1 0 2 -1)) | |
(run) | |
(exit) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Based on https://markushimmel.de/blog/my-first-verified-imperative-program/ | |
(defrule sum-is-0 | |
(list $?before ?first $?middle ?second $?) | |
(test (= 0 (+ ?first ?second))) | |
=> | |
(println "List has two values that sum to 0:") | |
(println ?first " (index " (length$ ?before) "), " ?second " (index " (+ 1 (length$ ?before) (length$ ?middle)) ")")) | |
(defrule sum-is-not-0 | |
(not (and | |
(list $?before ?first $?middle ?second $?) | |
(test (= 0 (+ ?first ?second))))) | |
=> | |
(println "List does not have two values that sum to 0 :(")) | |
(reset) | |
(assert (list 1 0 2 -1)) | |
(println "Test 1") | |
(run) | |
(println) | |
(reset) | |
(assert (list 3 0 2 -1)) | |
(println "Test 2") | |
(run) | |
(println) | |
(reset) | |
(assert (list 3 0 2 -1 4 9 2 1 -2)) | |
(println "Test 3") | |
(run) | |
(println) | |
(reset) | |
(assert (list 7 0 2 -1 4 9 2 1 -8 32 -4 7 2)) | |
(println "Test 4") | |
(run) | |
(println) | |
(exit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment