В моем распоряжении следующая реализация упорядоченной структуры:
;; os-empty an empty set
;; (os-singleton e) produces a set with one element e
;; (os-member s e) produces true if e is a member of s; false otherwise
;; (os-union s t) produces union of s and t in time
;; O(min(|s|,|t|) log max(|s|,|t|)
;; (os-intersection s t) produces intersection of s and t in time
;; O(min(|s|,|t|) log max(|s|,|t|)
;; (os-difference s t) produces difference s \ t in time
;; O(min(|s|,|t|) log max(|s|,|t|)
;; (os-min s) produces the to-min element of s, or to-min-ident
;; running time: O(log |s|)
;; (os-max s) produces the to-max element of s, or to-max-ident
;; running time: O(log |s|)
;; (os-after s e) produces the to-min element of s which is to> than e
;; running time: O(log |s|)
;; (os-before s e) produces the to-max element of s which is to< than e
;; running time: O(log |s|)
;; (os-op) produces the result of applying to-op over all e in s
;; running time: O(1)
Код можно найти в следующих файлах: https://www.student.cs.uwaterloo.ca/~cs136/assignments/a8/ordered-set.rkt и https://www.student.cs.uwaterloo.ca/~cs136/assignments/a8/total-order.rkt
Структура не допускает дублирования в ней. Вот моя проблема: мне нужно написать программу, которая записывает каждое отдельное целое число в порядке возрастания, за которым следует пробел, а затем число раз, когда оно встречается на входе, в отдельной строке вывода.
Sample Input
1
3
2
1
Output for Sample Input
1 2
2 1
3 1
Вот что у меня есть:
;;Prints in ascending order
(define (printer e s)
(cond
[(equal? (to-unhide e) +inf.0) (printf "")]
[else (printf "~a\n" (to-unhide e)) (printer (os-after s e) s)]))
(define (main)
(define x (read))
(cond
[(eof-object? x) (printer (os-min o) o)]
[(os-member o (os-singlton (to-hide x))) ....... <--- What to do?
[else (set! o (os-union (os-singleton (to-hide x)) o)) (main)]))
(main)
Моя проблема в том, как создать счетчик на основе x и как сделать этот счетчик особенным для x ... Я думал о создании функции, которая производит переменные, но я не думаю, что это возможно для Scheme. Любые предложения, как запомнить количество раз, когда вход был использован с использованием этой структуры?