Printing 有序集表示法

Printing 有序集表示法,printing,scheme,Printing,Scheme,我可以使用以下有序结构的实现: ;; 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 ;;

我可以使用以下有序结构的实现:

;; 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)
以下是我得到的:

 ;;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)
;;按升序打印
(定义(打印机e s)
(续)
[(等于?(取消隐藏e)+inf.0)(printf”“)]
[else(printf“~a\n”(取消隐藏e))(打印机(s e后的操作系统)])
(定义(主)
(定义x(读取))
(续)
[(eof对象?x)(打印机(操作系统最小值o)o)]

[(操作系统成员o(操作系统singlton(隐藏x))….将任务分成更小的块。 下面是一种将任务划分为更小功能的方法

  • 写入列表->有序集:数字列表->有序集 将中的数字列表转换为有序集的

  • 写读数字:字符串列表->数字列表 将字符串列表转换为数字列表的

  • 写入读取所有行:->字符串列表 重复使用读取行直到看到eof, 并返回所有已读行的列表

  • 在(列表->有序集(读取数字(读取所有行)))上使用打印机


  • 我不能使用列表,只使用我在问题开始时提供的功能。然后将数字1和2组合成一个函数。但是,呃,你知道在列表中读取的数字不会对你的运行时间产生负面影响。我还会使用列表吗?提供的结构不会向结构中添加重复项。这就是为什么我没有这样做的原因行
    (os member o(os singlton(隐藏x))
    检查x是否已经在o中,如果是,我只想像计数器一样添加到x中,但我不知道如何做..如果集合S包含元素e,那么S U{e}=S,因此你不需要检查e是否已经在S中。简单地做一个以空集开始的循环,然后一次添加一个数字,而不检查重复项。我仍然对你的策略有点迷茫。假设我输入数字4,两次,数字2,三次……第二次输入4,你想让我再创建一个吗r结构?一次“一个数字”是什么意思?
     ;;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)