在Lisp中计算分数平均值和班级平均值

在Lisp中计算分数平均值和班级平均值,lisp,common-lisp,average,Lisp,Common Lisp,Average,我正在研究一个问题,要求我计算班级中每个学生的平均分数 输入是具有以下格式的lisp文件: ( ((name studentname) (class hour grade) (class hour grade) ...) ((name studentname) (class hour grade) (class hour grade) ...) ...) 对于输出:我需要打印学生姓名和他们的GPA(该学生的平均成绩),按平均成绩以及班级平均成绩(每个独特班级的平均成绩)排序 到目前为止,这就是

我正在研究一个问题,要求我计算班级中每个学生的平均分数

输入是具有以下格式的lisp文件:

( ((name studentname) (class hour grade) (class hour grade) ...)
((name studentname) (class hour grade) (class hour grade) ...) ...)
对于输出:我需要打印学生姓名和他们的GPA(该学生的平均成绩),按平均成绩以及班级平均成绩(每个独特班级的平均成绩)排序

到目前为止,这就是我所拥有的

(setq class '(((name Seymore) (eng 3 4.0) (mat 3 3.0) (his 3 4.0) (bio 3 2.0) (biol 1 4.0))
 ((name Ichahbod) (cs 3 3.0) (mat 3 4.0) (spe 2 4.0) (che 3 4.0) (chel 1 3.0) (lit 3 3.0))
 ((name Zackery) (mat 5 3.0) (eng 3 3.0) (jou 2 3.0) (phy 3 3.0) (phyl 1 4.0) (lit 2 4.0))
 ((name Tukerville) (soc 4 3.0) (mus 2 4.0) (jou 3 4.0) (geo 4 4.0) (geol 1 3.0) (eng 3 3.0))
 ((name Simonsays) (css 3 3.0) (ast 3 4.0) (spe 3 4.0) (cs 3 4.0) (spe 2 3.0) (dan 4 4.0))
 ((name Snicker) (eng 3 4.0) (phy 4 4.0) (css 3 2.0) (csl  1 4.0) (ped 2 3.0) (mat 3 3.0))
 ((name Glass) (mat 3 1.0) (eng 3 1.0) (ped 1 1.0) (bio 3 1.0) (biol 1 0.0) (che 3 1.0) (chel 1 1.0))))

;this function multiplies the hours * the grades
(defun product (hours grades)
   (* hours grades)
)

;this function multiplies a set of grades
(defun sumofGrades (L)
    (cond
       ((null L) 0) ;check if it is first
       (t (+ (product (cdr (cdadar L)) (caddar L)))) ;first val then the second val
       (sumofGrades (cdr L)) ;the rest of one
    )
)

;to get the total , same as sum of grades but sum the second variables
(defun totalHours (L) 
    (cond
       ((null L) 0) ;check if it is first
       (t (+ (product (caddar L) (caddar L)))) ;first val then the second val
       (totalHours() (cdr L)) ;the rest of one
    )
 )


(defun gradepoint (L)
   ( / (sumofGrades L) (totalHours L))
)
我试图从辅助方法开始,因为我认为这是最好的方法,可能不是。当我运行sumofGrades时,我从第一个条目中得到了所需的4.0,但它说这不是一个数字。我写的这些方法是从我需要用数字做的基础数学开始的,但现在我不知道下一步该怎么做

如果我需要倒带,并去一个不同的例行我下来,任何帮助将不胜感激

您可能想试试:

然后,您可以使用以下方法对其进行排序:

(排序*#'<:键#'cdr)
==>
(格拉斯0.85714287)(扎克里3.3333333)(斯奈克3.3333333)(塞莫尔3.4)
(ICHAHBOD.3.5)(TUKERVILLE.3.5)(SIMONSAYS.3.667))
这是上一个表达式的值

注:由于这可能是h/w,我给出的是一个代码示例,而不是一个完整的解决方案,因此我建议您使用我的代码,如果有不清楚的地方,请询问另一个非常具体的问题

PPS。一些风格上的评论:

  • 不要定义像您的
    产品那样的功能,这只是令人困惑的噪音
  • 不要使用CamelCase,而是使用普通的lisp破折号
  • 不要使用悬挂式护腿
  • 使用Emacs缩进代码,它现在不可读
  • 您可能想试试:

    然后,您可以使用以下方法对其进行排序:

    (排序*#'<:键#'cdr)
    ==>
    (格拉斯0.85714287)(扎克里3.3333333)(斯奈克3.3333333)(塞莫尔3.4)
    (ICHAHBOD.3.5)(TUKERVILLE.3.5)(SIMONSAYS.3.667))
    
    这是上一个表达式的值

    注:由于这可能是h/w,我给出的是一个代码示例,而不是一个完整的解决方案,因此我建议您使用我的代码,如果有不清楚的地方,请询问另一个非常具体的问题

    PPS。一些风格上的评论:

  • 不要定义像您的
    产品那样的功能,这只是令人困惑的噪音
  • 不要使用CamelCase,而是使用普通的lisp破折号
  • 不要使用悬挂式护腿
  • 使用Emacs缩进代码,它现在不可读

  • 您的代码

    (defun sumofGrades (L)
      (cond
       ((null L) 0) ;check if it is first
       (t (+ (product (cdr (cdadar L)) (caddar L)))) ;first val then the second val
       (sumofGrades (cdr L)) ;the rest of one
       )
      )
    
    让我们看看它

    (defun sumofGrades (L)    ; please no camelCase in Lisp
    
      (cond
    
    
       ((null L) 0) ;check if it is first    <-  what does this comment mean???
    
    
       (t (+ (product (cdr (cdadar L)) (caddar L))))
    
       ;  what is (+ (product (cdr (cdadar L)) (caddar L))) ?
       ;  you are calling + with one argument. Why?
    
       ;  what does a function like caddar mean?
       ;  what is it supposed to do?
       ;  no one reading your code will have an idea why
       ;    caddar and not cdaadar, cdadaadr, or cdddddr...
       ;  write better documented, or self-documenting code.
    
    
       (sumofGrades (cdr L)) ;the rest of one    <- what does this comment mean?
    
       ; what is (sumofGrades (cdr L)) ?
       ; is   sumofGrades  a variable checked in COND?
       ; should it be a function call?
       ; just as it is alone here, it does not make any sense.
       ; since T is always true, this clause is also never reached...
    
       )    ;  <-  please no dangling parentheses in Lisp
      )
    
    总结
    总结
    不起作用。一个Lisp编译器已经在抱怨它了


    更多关于风格的信息

    (defun sumofGrades (L)
      (cond
       ((null L) 0) ;check if it is first
       (t (+ (product (cdr (cdadar L)) (caddar L)))) ;first val then the second val
       (sumofGrades (cdr L)) ;the rest of one
       )
      )
    
    全局变量:它们由DEFPARAMETER或DEFVAR定义。不要使用
    SETQ

    不要写

    (setq class ...)
    
    改为写:

    (defparameter *class* ...
       "the global variable *class* is a list of ...")
    

    您的代码

    (defun sumofGrades (L)
      (cond
       ((null L) 0) ;check if it is first
       (t (+ (product (cdr (cdadar L)) (caddar L)))) ;first val then the second val
       (sumofGrades (cdr L)) ;the rest of one
       )
      )
    
    让我们看看它

    (defun sumofGrades (L)    ; please no camelCase in Lisp
    
      (cond
    
    
       ((null L) 0) ;check if it is first    <-  what does this comment mean???
    
    
       (t (+ (product (cdr (cdadar L)) (caddar L))))
    
       ;  what is (+ (product (cdr (cdadar L)) (caddar L))) ?
       ;  you are calling + with one argument. Why?
    
       ;  what does a function like caddar mean?
       ;  what is it supposed to do?
       ;  no one reading your code will have an idea why
       ;    caddar and not cdaadar, cdadaadr, or cdddddr...
       ;  write better documented, or self-documenting code.
    
    
       (sumofGrades (cdr L)) ;the rest of one    <- what does this comment mean?
    
       ; what is (sumofGrades (cdr L)) ?
       ; is   sumofGrades  a variable checked in COND?
       ; should it be a function call?
       ; just as it is alone here, it does not make any sense.
       ; since T is always true, this clause is also never reached...
    
       )    ;  <-  please no dangling parentheses in Lisp
      )
    
    总结
    总结
    不起作用。一个Lisp编译器已经在抱怨它了


    更多关于风格的信息

    (defun sumofGrades (L)
      (cond
       ((null L) 0) ;check if it is first
       (t (+ (product (cdr (cdadar L)) (caddar L)))) ;first val then the second val
       (sumofGrades (cdr L)) ;the rest of one
       )
      )
    
    全局变量:它们由DEFPARAMETER或DEFVAR定义。不要使用
    SETQ

    不要写

    (setq class ...)
    
    改为写:

    (defparameter *class* ...
       "the global variable *class* is a list of ...")
    

    首先定义一些通用平均函数:

    (defun average (lst &key (key #'identity)) 
      (when lst
        (/ (reduce #'+ lst :key key) (length lst))))
    
    还定义一个grade函数来检索给定类中给定学生的成绩(不需要,但会更清楚):

    以及用于检索学生成绩的成绩函数:

    (defun grades (student) 
        (cdr (find student class :key #'cadar)))
    
    现在你可以通过打电话找到一个学生的平均成绩

    (average (grades 'seymore ) :key #'grade) 
    => 3.4
    

    根据本例,您应该能够自己编写所有类的平均值。

    首先定义一些通用平均值函数:

    (defun average (lst &key (key #'identity)) 
      (when lst
        (/ (reduce #'+ lst :key key) (length lst))))
    
    还定义一个grade函数来检索给定类中给定学生的成绩(不需要,但会更清楚):

    以及用于检索学生成绩的成绩函数:

    (defun grades (student) 
        (cdr (find student class :key #'cadar)))
    
    现在你可以通过打电话找到一个学生的平均成绩

    (average (grades 'seymore ) :key #'grade) 
    => 3.4
    

    在本例中,您应该能够自己编写所有类的平均值。

    下次请粘贴完整的错误消息和函数调用。好的,对此我深表歉意。下次请粘贴完整的错误消息和函数调用。好的,我很抱歉。谢谢您的回复。我使用cdadar、cdr和其他函数从不同的列表中获取等级an小时数。我想从本质上获取单个数字,然后将它们相加,再将它们相乘得到乘积。我的教授在这个代码上帮助了一些人,所以我靠他吃饭。我对lisp很陌生,所以我为语法上的怪癖道歉。@user2762848:问题是你永远不会记得CADAR的意思。如果它意味着GET-HOURS,那么使用函数GET-HOURS。如果它的意思是别的,那就把它命名为别的。但是给它一个有用的名字。你可能需要写下来…谢谢你的回复。我使用cdadar、cdr和其他函数从不同的列表中获取等级an小时数。我想从本质上获取单个数字,然后将它们相加,再将它们相乘得到乘积。我的教授在这个代码上帮助了一些人,所以我靠他吃饭。我对lisp很陌生,所以我为语法上的怪癖道歉。@user2762848:问题是你永远不会记得CADAR的意思。如果它意味着GET-HOURS,那么使用函数GET-HOURS。如果它的意思是别的,那就把它命名为别的。但是给它一个有用的名字。你可能需要写它…非常感谢你的解释。我对lisp很陌生,这对我帮助很大。我将根据您的建议更正我的代码,如果有问题,我将通知您。再次感谢你。非常感谢你的解释。我对lisp很陌生,这对我帮助很大。我将根据您的建议更正我的代码,如果有问题,我将通知您。再次感谢。太棒了,非常感谢