mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-28 18:57:48 +01:00
cities path
This commit is contained in:
90
clitests/cities.psil
Normal file
90
clitests/cities.psil
Normal 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
20
clitests/cities.psil.ex
Normal 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
|
||||
Reference in New Issue
Block a user