How to resolve the algorithm Set of real numbers step by step in the Racket programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Set of real numbers step by step in the Racket programming language
Table of Contents
Problem Statement
All real numbers form the uncountable set ℝ. Among its subsets, relatively simple are the convex sets, each expressed as a range between two real numbers a and b where a ≤ b. There are actually four cases for the meaning of "between", depending on open or closed boundary: Note that if a = b, of the four only [a, a] would be non-empty. Task Implementation notes Optional work |sin(π x)| > 1/2 is the same as n + 1/6 < x < n + 5/6 for all integers n; your program does not need to derive this by itself.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Set of real numbers step by step in the Racket programming language
Source code in the racket programming language
#lang racket
;; Use a macro to allow infix operators
(require (only-in racket [#%app #%%app]))
(define-for-syntax infixes '())
(define-syntax (definfix stx)
(syntax-case stx ()
[(_ (x . xs) body ...) #'(definfix x (λ xs body ...))]
[(_ x body) (begin (set! infixes (cons #'x infixes)) #'(define x body))]))
(define-syntax (#%app stx)
(syntax-case stx ()
[(_ X op Y)
(and (identifier? #'op) (ormap (λ(o) (free-identifier=? #'op o)) infixes))
#'(#%%app op X Y)]
[(_ f x ...) #'(#%%app f x ...)]))
;; Ranges: (X +-+ Y) => [X,Y]; (X --- Y) => (X,Y); and same for `+--' and `--+'
;; Simple implementation as functions
;; Constructors
(definfix ((+-+ X Y) n) (<= X n Y)) ; [X,Y]
(definfix ((--- X Y) n) (< X n Y)) ; (X,Y)
(definfix ((+-- X Y) n) (and (<= X n) (< n Y))) ; [X,Y)
(definfix ((--+ X Y) n) (and (< X n) (<= n Y))) ; (X,Y]
(definfix ((== X) n) (= X n)) ; [X,X]
;; Set operations
(definfix ((∪ . Rs) n) (ormap (λ(p) (p n)) Rs))
(definfix ((∩ . Rs) n) (andmap (λ(p) (p n)) Rs))
(definfix ((∖ R1 R2) n) (and (R1 n) (not (R2 n)))) ; set-minus, not backslash
(define ((¬ R) n) (not (R n)))
;; Special sets
(define (∅ n) #f)
(define (ℜ n) #t)
(define-syntax-rule (try set)
(apply printf "~a => ~a ~a ~a\n" (~s #:width 23 'set)
(let ([pred set]) (for/list ([i 3]) (if (pred i) 'Y 'N)))))
(try ((0 --+ 1) ∪ (0 +-- 2)))
(try ((0 +-- 2) ∩ (1 --+ 2)))
(try ((0 +-- 3) ∖ (0 --- 1)))
(try ((0 +-- 3) ∖ (0 +-+ 1)))
You may also check:How to resolve the algorithm Show the epoch step by step in the Arturo programming language
You may also check:How to resolve the algorithm Closest-pair problem step by step in the RPL programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the FOCAL programming language
You may also check:How to resolve the algorithm Distance and Bearing step by step in the Phix programming language
You may also check:How to resolve the algorithm Box the compass step by step in the Wren programming language