Scheme 如何在drRacket中访问本地内的向量

Scheme 如何在drRacket中访问本地内的向量,scheme,racket,Scheme,Racket,我不知道这个问题是否适合我的问题,我是一个初学者,英语不是我的母语(很明显),很抱歉有任何错误 我有这个密码,是关于总统选举的,一个人必须输入姓名、性别和身份证号码,但它只能投票一次,所以同一身份证号码不能再次使用。我不知道怎么做 我尝试访问包含最后一位投票人信息的向量,并将其与当前投票人的信息进行比较。但是我不知道如何或者在哪里使用这个功能 (define (access_vec numId) (cond [(equal? (vector-ref

我不知道这个问题是否适合我的问题,我是一个初学者,英语不是我的母语(很明显),很抱歉有任何错误

我有这个密码,是关于总统选举的,一个人必须输入姓名、性别和身份证号码,但它只能投票一次,所以同一身份证号码不能再次使用。我不知道怎么做

我尝试访问包含最后一位投票人信息的向量,并将其与当前投票人的信息进行比较。但是我不知道如何或者在哪里使用这个功能

  (define (access_vec numId)
        (cond
             [(equal? (vector-ref vote 2) numId) "Type different Id"]
             [else (set! vote (vector name gender idNum))]))

我想知道我是如何做到这一点的:一个人只能投票一次。或者关于任何可以使此代码更好的提示。非常感谢。

我是初学者,英语也不是我的母语。 有很多方法可以解决这个问题。 你可以阅读racketdoc来理解这里的所有语法。总有一天你可以自己写所有的语法

在这里,我没有编写清晰的代码,因为逻辑很简单。 如果您在候选人选民发生变化时使用此代码,则无需更改任何代码

#lang racket
(define-struct person (name gender idNum voted-to) #:transparent #:mutable)

;;; data
(define candidate-list
  '(cand1 cand2 cand3))

(define person-list
  (list
   (person 'Mike 'man 0 #f)
   (person 'Mike1 'woman 1 #f)
   (person 'Mike2 'man 2 #f)
   (person 'Mike3 'woman 3 #f)
   (person 'Mike4 'man 4 #f)
   (person 'Bad1 'woman 5 #f)
   (person 'Bad2 'woman 6 #f)
   (person 'Bad3 'man 7 #f)
   (person 'Bad4 'orther 8 #f)
   (person 'Bad5 'orther 9 #f)))

;;; can-vote? : person -> boolean
(define (can-vote? a-person)
  (local ((define p-on-list
            (filter (lambda (p) (and (symbol=? (person-name p) (person-name a-person))
                                     (= (person-idNum p) (person-idNum a-person))
                                     (symbol=? (person-gender p) (person-gender a-person))))
                    person-list)))
    (cond
      ; in list?
      [(empty? p-on-list)
       #f]
      ; not vote yet?
      [(false? (person-voted-to (first p-on-list)))   
       #t]
      [else #f])))


;;; we don't check want-vote-cand legal or not
;;; because in real world also allow rejected ballot
;;; vote-machine : person symbol -> string
(define (vote-machine p want-vote-cand)
  (if (can-vote? p)
      (begin
        (map (lambda (p1) (if (= (person-idNum p) (person-idNum p1))
                              (set-person-voted-to! p1 want-vote-cand)
                              p1))
             person-list)
        "vote ok")
      "can't vote"))

;;; TEST
(vote-machine (person 'Mike 'man 0 #f) 'cand1)
(vote-machine (person 'Mike 'man 0 #f) 'cand2) ; vote twice
(vote-machine (person 'Mike1 'woman 1 #f) 'cand4) ; wrong vote
(vote-machine (person 'Mike2 'man 2 #f) 'bad-ticket) ; wrong vote
(vote-machine (person 'Mike3 'woman 3 #f) 'bad-ticket) ; wrong vote
(vote-machine (person 'Mike4 'man 4 #f) 'cand2) 
(vote-machine (person 'Bad1 'woman 5 #f) 'cand2) 
(vote-machine (person 'Bad2 'woman 6 #f) 'cand2)
(vote-machine (person 'Bad3 'woman 6 #f) 'cand2) ; wrong id
(vote-machine (person 'Bad3 'man 7 #f) 'cand3)
(vote-machine (person 'Bad3 'man 7 #f) 'cand1) ; vote twice
(vote-machine (person 'Bad3 'man 7 #f) 'cand2) ; vote twice

;;; TEST
; after voting person-list
person-list

;;; classification : listof-symbol listof-person -> listof-listof-person
(define (classification cand-list p-list result)
  (cond
    [(empty? cand-list)
     result]
    [else
     (classification
      (rest cand-list)
      p-list
      (cons (filter (λ (p) (equal? (first cand-list)
                                   (person-voted-to p)))
                    p-list) result))]))

(define (show-outcome candidate-list people-ls)
  (map (lambda (l1 l2)
         (list l2 "get" l1 "ticket"))
       (map length (classification candidate-list people-ls empty))
       candidate-list))

;;; TEST
(classification candidate-list person-list empty)
(show-outcome candidate-list person-list)

所有输出都应该是

"vote ok"
"can't vote"
"vote ok"
"vote ok"
"vote ok"
"vote ok"
"vote ok"
"vote ok"
"can't vote"
"vote ok"
"can't vote"
"can't vote"

; after voting person-list
(list
 (person 'Mike 'man 0 'cand1)
 (person 'Mike1 'woman 1 'cand4)
 (person 'Mike2 'man 2 'bad-ticket)
 (person 'Mike3 'woman 3 'bad-ticket)
 (person 'Mike4 'man 4 'cand2)
 (person 'Bad1 'woman 5 'cand2)
 (person 'Bad2 'woman 6 'cand2)
 (person 'Bad3 'man 7 'cand3)
 (person 'Bad4 'orther 8 #f)
 (person 'Bad5 'orther 9 #f))

; classification 
(list
 (list (person 'Bad3 'man 7 'cand3))
 (list
  (person 'Mike4 'man 4 'cand2)
  (person 'Bad1 'woman 5 'cand2)
  (person 'Bad2 'woman 6 'cand2))
 (list (person 'Mike 'man 0 'cand1)))

; show-outcome
'((cand1 "get" 1 "ticket")
  (cand2 "get" 3 "ticket")
  (cand3 "get" 1 "ticket"))
升级一个简单的版本

;;; data
(define person-name-ls
  (list 'Mike 'Mike1 'Mike2 'Mike3 'Mike4 'Mike5 'Mike6 'Mike7 'Mike8 'Mike9))

(define person-gender-ls
  (list 'man 'woman 'man 'woman 'man 'woman 'man 'woman 'man 'woman 'man))

(define person-id-ls
  '(0 1 2 3 4 5 6 7 8 9))

(define vote-result empty)

(define (person-check-ok? name gender id)
  (local ((define ref-posn (indexes-where person-id-ls (lambda (n) (equal? id n)))))
    (cond
      [(empty? ref-posn)
       #f]
      [else
       (and (symbol=? (list-ref person-name-ls (first ref-posn)) name)
            (symbol=? (list-ref person-gender-ls (first ref-posn)) gender))])))

(define (alread-vote id)
  (set! person-id-ls
        (map (λ (x) (if (equal? x id) #f x))
             person-id-ls)))

(define (vote-machine name gender id want-vote)
  (cond
    [(person-check-ok? name gender id)
     (begin
       (alread-vote id)
       (set! vote-result (cons want-vote vote-result))
       "vote finish")]
    [else "can't vote"]))

;;; TEST

(vote-machine 'Mike 'man 0 'cand1)
(vote-machine 'Mike1 'woman 1 'cand3)
(vote-machine 'Mike2 'man 2 'bad-ticket)
(vote-machine 'Mike3 'woman 3 'bad-ticket) 
(vote-machine 'Mike4 'man 4 'bad-ticket)
(vote-machine 'Mike5 'woman 5 'cand2) 
(vote-machine 'Mike6 'man 6 'cand2) 
(vote-machine 'Mike7 'woman 7 'cand3)
(vote-machine 'Mike8 'man 8 'cand3)
(vote-machine 'Mike1 'man 0 'cand2) ; vote twice
(vote-machine 'Mike3 'woman 3 'cand3) ; vote twice
(vote-machine 'Mike-x 'man 9 'cand2) ; wrong person data

(define (show-ticket-number candidate-name)
  (count (lambda (x) (symbol=? x candidate-name)) vote-result))

;;; TEST
(show-ticket-number 'cand1)
(show-ticket-number 'cand2)
(show-ticket-number 'cand3)

谢谢你抽出时间。你写的东西中有很多我不明白的地方,比如兰姆达。我是初学者,我知道球拍博士,但由于语言的原因,有很多东西我不懂。我会在网上搜索答案,看看如何处理你写的东西。再次感谢。f(x):=x^2(λ(x)(sqrx));f(2)(lambda(x)(sqr x))2 ;;g(x,y):=x^2-y(λ(xy)((sqrx)y));g(1,2)((lambda(xy)((sqrx)y))12)我只写了一个简单的版本@戴维杜姆
;;; data
(define person-name-ls
  (list 'Mike 'Mike1 'Mike2 'Mike3 'Mike4 'Mike5 'Mike6 'Mike7 'Mike8 'Mike9))

(define person-gender-ls
  (list 'man 'woman 'man 'woman 'man 'woman 'man 'woman 'man 'woman 'man))

(define person-id-ls
  '(0 1 2 3 4 5 6 7 8 9))

(define vote-result empty)

(define (person-check-ok? name gender id)
  (local ((define ref-posn (indexes-where person-id-ls (lambda (n) (equal? id n)))))
    (cond
      [(empty? ref-posn)
       #f]
      [else
       (and (symbol=? (list-ref person-name-ls (first ref-posn)) name)
            (symbol=? (list-ref person-gender-ls (first ref-posn)) gender))])))

(define (alread-vote id)
  (set! person-id-ls
        (map (λ (x) (if (equal? x id) #f x))
             person-id-ls)))

(define (vote-machine name gender id want-vote)
  (cond
    [(person-check-ok? name gender id)
     (begin
       (alread-vote id)
       (set! vote-result (cons want-vote vote-result))
       "vote finish")]
    [else "can't vote"]))

;;; TEST

(vote-machine 'Mike 'man 0 'cand1)
(vote-machine 'Mike1 'woman 1 'cand3)
(vote-machine 'Mike2 'man 2 'bad-ticket)
(vote-machine 'Mike3 'woman 3 'bad-ticket) 
(vote-machine 'Mike4 'man 4 'bad-ticket)
(vote-machine 'Mike5 'woman 5 'cand2) 
(vote-machine 'Mike6 'man 6 'cand2) 
(vote-machine 'Mike7 'woman 7 'cand3)
(vote-machine 'Mike8 'man 8 'cand3)
(vote-machine 'Mike1 'man 0 'cand2) ; vote twice
(vote-machine 'Mike3 'woman 3 'cand3) ; vote twice
(vote-machine 'Mike-x 'man 9 'cand2) ; wrong person data

(define (show-ticket-number candidate-name)
  (count (lambda (x) (symbol=? x candidate-name)) vote-result))

;;; TEST
(show-ticket-number 'cand1)
(show-ticket-number 'cand2)
(show-ticket-number 'cand3)