【问题标题】:data definitions in scheme?方案中的数据定义?
【发布时间】:2013-01-28 10:21:54
【问题描述】:

我今天一直在处理这个问题,我剩下要做的就是开发一个函数,该函数需要一名服务员并返回服务员被分配到的动物的总年龄。因此,如果我以服务员 Dave 为例,我会得到 10 个。不知道从哪里开始。您如何将年龄相加?

(define-struct animal (name species age breakfasthour dinnerhour))
    (define-struct attendant (name a1 a2 a3))


    (define gorilla (make-animal "Koko" "Gorilla" "4" "8" "10"))
    (define bat (make-animal "Bruce" "Bat" "1" "23" "5"))
    (define mandrill (make-animal "Manny" "Mandrill" "5" "8" "7"))
    (define crocodile (make-animal "Swampy" "Crocodile" "1" "10" "8"))
    (define ocelot (make-animal "Ozzy" "Ocelot" "7" "7" "17"))
    (define capybara (make-animal "Capy" "Capybara" "4" "6" "8"))
    (define potto (make-animal "Spot" "Potto" "2" "2" "6"))
    (define tapir (make-animal "Stripey" "Tapir" "3" "10" "6"))
    (define vulture (make-animal "Beaky" "Vulture" "10" "9" "6"))


    (define attendant1 (make-attendant "Dave" gorilla bat mandrill))
    (define attendant2 (make-attendant "John" crocodile ocelot capybara))
    (define attendant3 (make-attendant "Joe" potto tapir vulture))


    #;(define (meal-time? e1 e2)
      (string=? (animal-species e1)
                (animal-dinnerhour e2)))



    #;(define (animal-template s...)
      (...(animal-name s)...
          (animal-species s)...
          (animal-age s)...
          (animal-breakfasthour s)...
          (animal-dinnerhour s)...))

    #;(define (attendant-template s...)
      (...(attendant-name s)...
          (attendant-s1 s)...
          (attendant-s2 s)...
          (attendant-s3 s)...))

【问题讨论】:

    标签: scheme racket


    【解决方案1】:

    只需使用每种类型的存取程序:

    (define (animals-age att)
      (+ (animal-age (attendant-a1 att))
         (animal-age (attendant-a2 att))
         (animal-age (attendant-a3 att))))
    

    显然,这仅适用于年龄是数字,在您当前的代码中是一个字符串(为什么?),请考虑将年龄表示为数字,这更有意义。

    【讨论】:

      猜你喜欢
      • 2014-05-30
      • 1970-01-01
      • 2014-04-18
      • 2013-01-08
      • 2023-03-06
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 2019-01-23
      相关资源
      最近更新 更多