cities path

This commit is contained in:
2024-01-03 22:47:39 +01:00
parent 009218c6f9
commit d163bb0ac0
6 changed files with 173 additions and 24 deletions

90
clitests/cities.psil Normal file
View File

@@ -0,0 +1,90 @@
(define (equal? a b)
(if (nil? a) (if (nil? b) 1 0)
(if (atom a)
(if (atom b) (= a b) 0)
(if (atom b) 0 (if (equal? (car a) (car b)) (equal? (cdr a) (cdr b)) 0))
)
)
)
(define (empty? l)
(equal? l (nil))
)
(define (not a)
(if a 0 1)
)
(not 2)
(not 1)
(not 0)
(define (lhas list what)
(if (empty? list) 0
(if (equal? (car list) what) 1 (lhas (cdr list) what))
)
)
(define (onehas list l)
(if (empty? list) 0
(if (l (car list)) 1 (onehas (cdr list) l))
)
)
(define (appfront list what)
(cons what list)
)
(define (pathslist acc paths from)
(if (empty? paths)
acc
(if (equal? (car (car paths)) from)
(pathslist (appfront acc (car(cdr (car paths)))) (cdr paths) from)
(pathslist acc (cdr paths) from)
)
)
)
(equal? (pathslist () (quote((1 2))) 1) (quote(2)))
(define (filter p l)
(if (empty? l) (nil)
(if (p (car l))
(cons (car l) (filter p (cdr l)) )
(filter p (cdr l))
)
))
(filter (lambda (x) (if (> x 0) 1 0 )) (quote(-1 2 0 3 -3 1)))
(equal? (filter (lambda (x) (if (> (car x) 0) (if (> (car(cdr x)) 0) 1 0) 0 )) (quote((-1 2) (0 3) (-3 1) (1 1)))) (quote((1 1))))
(define (cities-path-impl seen paths from to)
(if (equal? from to)
1
(onehas
(filter (lambda (w) (not (lhas (appfront seen from) w))) (pathslist (nil) paths from))
(lambda (f) (cities-path-impl (appfront seen from) paths f to))
)
)
)
(define (cities-path? paths from to)
(cities-path-impl (nil) paths from to)
)
(cities-path? (nil) 1 2)
(cities-path? (nil) 2 2)
(cities-path? (quote((1 2))) 1 2)
(cities-path? (quote((1 2))) 1 3)
(cities-path? (quote((1 2))) 2 1)
(cities-path? (quote((2 1))) 1 2)
(cities-path? (quote((1 2) (2 1))) 1 2)
(cities-path? (quote((1 2) (2 1))) 2 1)
(cities-path? (quote((1 2) (2 3) (3 4) (4 2))) 1 2)
(cities-path? (quote((1 2) (2 3) (3 4) (4 2))) 1 3)
(cities-path? (quote((1 2) (2 3) (3 4) (4 2))) 1 4)
(cities-path? (quote((1 2) (2 3) (3 4) (4 2))) 2 3)
(cities-path? (quote((1 2) (2 3) (3 4) (4 2))) 2 4)
(cities-path? (quote((4 2) (3 4) (2 3) (1 2))) 3 2)

20
clitests/cities.psil.ex Normal file
View File

@@ -0,0 +1,20 @@
0
0
1
1
(2 3 1)
1
0
1
1
0
0
0
1
1
1
1
1
1
1
1