-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathE16.rkt
66 lines (52 loc) · 1.6 KB
/
E16.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname E16) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #t)))
;;
;; ***************************************************
;; James Ah Yong
;; CS 135 Fall 2020
;; Module 16 Exercises
;; ***************************************************
;;
;; A Node is a Sym
;; A Graph is one of:
;; * empty
;; * (cons (list v (list w_1 ... w_n)) g)
;; where g is a Graph
;; v, w_1, ... w_n are Nodes
;; v is the in-neighbour to w_1 ... w_n in the Graph
;; v does not appear as an in-neighbour in g
;;
;; Exercise 1
;;
;; (count-out-neighbours g) counts how many out-neighbours each node in g has
;; Example:
(check-expect (count-out-neighbours g) '(3 2 0 2 1 2 0 1 0))
;; count-out-neighbours: Graph -> (listof Nat)
(define (count-out-neighbours g)
(map (lambda (n) (length (second n))) g))
;;
;; Exercise 2
;;
;; (count-in-neighbours g) counts how many in-neighbours each node in g has
;; Example:
(check-expect (count-in-neighbours g) '(0 0 1 1 2 1 2 2 2))
;; count-in-neighbours: Graph -> (listof Nat)
(define (count-in-neighbours g)
(map (lambda (n)
(length (filter (lambda (node) (member? (first n) (second node))) g)))
g))
;;
;; Testing constants
;;
(define g
'((A (C D E))
(B (E J))
(C ())
(D (F J))
(E (K))
(F (K H))
(H ())
(J (H))
(K ()))
)