返回 動漫遊戲

(求救) 電腦程式語言  (Scheme) 有人會嗎??

有三題考古題裡面  發現不會做的問題 

第一:
;;  TYPE:  list<X>  ->  list<pair<X,  integer>>
;;  EFFECTS:  =>  the  list  of  pairs  of  the  form  (cons  x  xc)
;;  where  x  is  an  element  of  xs  and  xc  is  its  frequency.
;;  The  result  does  not  contain  repeated  elements  -  i.e.
;;  each  element  of  xs  is  processed  once.


例:  (freq-all  ’(7  8  9  7  8  7))
                                                                                                        =>  ((7  .  3)  (8  .  2)  (9  .  1))

我的答案:

(define  (num-in-list  x  xs)
    (length  (filter  (lambda  (n)  (eq?  x  n))  xs)))

(define  (list-without  xs  x)
    (filter  (lambda  (y)  (not  (eq?  x  y)))  xs))

(define  (freq-first  xs)
    (let  ((x  (car  xs)))
    (cons  (cons  x  (num-in-list  x  xs))
                                (list-without  xs  x))))

(define  (freq-all  xs)
    (if  (null?  xs)  null
    (cons  (freq-first  xs)
                (freq-all  (cdr  xs)))))

可是答案不對    我的程式出來是    ((7  .  3)  (8  .  2)  (9  .  1)  (8.1)  (7.1))

  • 第二題:

    (define  (text->frequencies  str)
    ;;  TYPE:  string  ->  list<pair<char,  integer>>
    ;;  EFFECTS:  =>  The  sorted  list  (in  decreasing  frequency  order)
    ;;  of  the  elements  of  str  and  their  frequencies.

    e.g  (text->frequencies  "hello  world")  =>
    ((#l  .  3)  (#o  .  2)  (#d  .  1)  (#w  .  1)
    (#e  .  1)  (#r  .  1)  (#space  .  1)  (#h  .  1))


    這題完全不會...  照上面那題  的方法做    出來的答案是  ((hello  .  1)  (world  .  1))

    --------------------------------------------------------------------------------------------------
    第三題:

    (iterative  version)

    ;;  TYPE:  X,  list<X>  ->  integer
    ;;  EFFECTS:  =>  the  number  of  occurrences  of  x  in  xs.


    ;;  e.g.  (num-in-list  3  ’(1  2  3  4  3  2  1))  =>  2
    ;;  (num-in-list  5  ’(1  2  3  4  3  2  1))  =>  0


    這題我只會用  length  跟  filter  做  而問題規定不能用
    PS.  需要用  iterative  的方法寫 
  • nmomtf的話:
    請問    ...    你也順便和    LISP    在奮戰嗎?    O_O
    ===============================================

    LISP  阿? 

    學校有放一個  九頁  的  document 在網路上..

    因為都是字..  所以就懶得看了~  哈哈~
回應...
 返回 動漫遊戲