Function body for a function that consumes a compound data list of universities and produces the name of school with the lowest tuition. BSL RACKET

huangapple go评论60阅读模式
英文:

Function body for a function that consumes a compound data list of universities and produces the name of school with the lowest tuition. BSL RACKET

问题

(define (lowest? lou)
(cond [(empty? lou) "none"]
[(empty? (rest lou)) (uni-name (first lou))]
[else (if (< (uni-tuition (first lou)) (uni-tuition (lowest? (rest lou))))
(uni-name (first lou))
(lowest? (rest lou)))]))

英文:

Hello everyone Im having trouble coding the body for a function that
consumes a compound data list of universities and produces the name of the university with the lowest tuition.

The language is BSL Racket.

Compound Data Definition:

(define-struct uni (name tuition))

interp. a university name as a string and its tuition as a 
natural

(define UNI-1 (make-uni &quot;UBC&quot; 28500))
; example of compound data definition

The List Data Definition For The Universities:

ListOfUni is one of:
  - empty
  - (cons uni ListOfUni)
 interp. a list of unis

(cons (make-uni &quot;SFU&quot; 27797) (cons (make-uni &quot;UVIC&quot; 26000) 
    empty))

Function Introduced:

 (lowest? lou) 

This is a function that consumes list of unis and produces the 
name of school with the lowest tuition 

The Check-Expects:

(check-expect (lowest? empty) &quot;none&quot;)
(check-expect (lowest? (cons (make-uni &quot;UBC&quot; 28500) empty)) 
&quot;UBC&quot;)
(check-expect (lowest? (cons (make-uni &quot;SFU&quot; 27797) (cons (make- 
uni &quot;UVIC&quot; 26000) empty))) &quot;UVIC&quot;)

Attempt at function body:

 (define (lowest? lou)
  (cond [(empty? lou) &quot;none&quot;]
        [(empty? (rest lou)) (uni-name (first lou))]
        [else (if  (&lt;(uni-tuition (first lou)) (uni-tuition (lowest? (rest lou))))
                (uni-name (first lou))
                (lowest? (rest lou)))]))

Error Message Given:

1 of the 3 tests failed.

Check failures:
 check-expect encountered the following error instead of the 
expected value, &quot;UVIC&quot;. 
   :: uni-tuition: expects an uni, given &quot;UVIC&quot;

I don't understand how to get around this error and still have the code be recursive.

Also Please excuse any formatting errors, this is my first post on StackOverFlow

答案1

得分: 0

在迭代大学列表时,你必须从每个步骤中返回uni结构,因为你在结果上调用了uni-tuition(uni-tuition (lowest? (rest lou)))

但当lou只有一个元素时,你的函数会返回(uni-name (first lou)),这是一个字符串。这就是错误消息的含义::: uni-tuition: 期望一个uni,但给定了"UVIC"

可能有更多解决这个练习的方法,但由于BSL的限制(没有letorand仅适用于#true#false值),我不得不想出一个帮助函数和用(make-uni "none" 0)创建的"none"大学:

(define-struct uni (name tuition))

(define (lowest-helper lou)
  (cond [(empty? lou) (make-uni "none" 0)]
        [(empty? (rest lou)) (first lou)]
        [(< (uni-tuition (first lou))
            (uni-tuition (lowest-helper (rest lou))))
         (first lou)]
        [else
         (lowest-helper (rest lou))]))

(define (lowest? lou)
  (uni-name (lowest-helper lou)))

(check-expect (lowest? empty) "none")
(check-expect (lowest? (cons (make-uni "UBC" 28500) empty)) "UBC")
(check-expect (lowest? (cons (make-uni "SFU" 27797)
                             (cons (make-uni "UVIC" 26000) empty))) "UVIC")

或者你可以返回要么是"none"要么是uni结构,并添加一个新的函数来区分它们:

(define (get-name result)
  (if (string? result)
      result
      (uni-name result)))

(define (lowest? lou)
  (get-name (lowest-helper lou)))
英文:

When you're iterating over the list of universities, you have to return uni struct from each step, because you call uni-tuition on the result: (uni-tuition (lowest? (rest lou)))

But when lou has only one element, your function returns (uni-name (first lou)) and that is a string. That is the meaning of the error message: :: uni-tuition: expects an uni, given "UVIC"

There are probably more ways to solve this exercise, but given BSL limitations (no let, or and and work only for #true and #false values), I had to come up with helper function and "none" university created with (make-uni &quot;none&quot; 0):

(define-struct uni (name tuition))

(define (lowest-helper lou)
  (cond [(empty? lou) (make-uni &quot;none&quot; 0)]
        [(empty? (rest lou)) (first lou)]
        [(&lt; (uni-tuition (first lou))
            (uni-tuition (lowest-helper (rest lou))))
         (first lou)]
        [else
         (lowest-helper (rest lou))]))

(define (lowest? lou)
  (uni-name (lowest-helper lou)))

(check-expect (lowest? empty) &quot;none&quot;)
(check-expect (lowest? (cons (make-uni &quot;UBC&quot; 28500) empty)) 
&quot;UBC&quot;)
(check-expect (lowest? (cons (make-uni &quot;SFU&quot; 27797)
                             (cons (make-uni &quot;UVIC&quot; 26000) empty))) &quot;UVIC&quot;)

Or you can return either &quot;none&quot; or uni struct and add a new function to differentiate between them:

(define (get-name result)
  (if (string? result)
      result
      (uni-name result)))

(define (lowest? lou)
  (get-name (lowest-helper lou)))

huangapple
  • 本文由 发表于 2023年2月18日 15:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定