Frontier Software

Robert Laing's programing notes

Racket

By Robert Laing

S-expressions

https://htdp.org/2020-8-1/Book/i2-3.html

https://www.youtube.com/watch?v=6QsTfL-uXd8

Mutual Recursion

Racket’s structures are fairly longwinded next to Prolog’s predicates. Structures need separate functions to define, create, and retrieve fields.

;; Data definitions:

(define-struct elt (name data subs))
;; Element is (make-elt String Integer ListOfElement)
;; interp. An element in the file system, with name, and EITHER data or subs.
;;         If data is 0, then subs is considered to be list of sub elements.
;;         If data is not 0, then subs is ignored.

;; ListOfElement is one of:
;;  - empty
;;  - (cons Element ListOfElement)
;; interp. A list of file system Elements

(define F1 (make-elt "F1" 1 empty))
(define F2 (make-elt "F2" 2 empty))
(define F3 (make-elt "F3" 3 empty))
(define D4 (make-elt "D4" 0 (list F1 F2)))
(define D5 (make-elt "D5" 0 (list F3)))
(define D6 (make-elt "D6" 0 (list D4 D5)))

#;
(define (fn-for-element e)
  (... (elt-name e)   ; String
       (elt-data e)   ; Natural
       (fn-for-loe (elt-subs e)))) ; ListOfElement
#;
(define (fn-for-loe loe)
  (cond [(empty? loe) (...)]
        [else
         (... (fn-for-element (first loe))
              (fn-for-loe (rest loe)))]))
Last updated on 7 Jan 2021
Published on 7 Jan 2021

Content Footer