方案查找列表的平均值 [英] Scheme find average of a list

查看:57
本文介绍了方案查找列表的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的代码应该可以找到列表的平均值,但我在控制台中没有看到打印值

So I have code that should find the average of a list but I see no printed value in console

#lang racket
(define x (list '10 '60 '3 '55 '15 '45 '40))
(define (average x)
(/ (sum x) (length x)))

(display average (current-output-port))

(define (sum)
(if (null? x)
    0
    (+ (car x) (sum (cdr x)))))

它只是显示

#<procedure:average>

推荐答案

您的代码有以下问题:

  1. sum 在定义之前使用.
  2. sum 没有带参数.
  3. average 函数未在 display 中求值.
  4. 我使用了 exact->inexact 因为这就是我认为您的意图.
  1. sum was used before it was defined.
  2. sum did not take a parameter.
  3. average function was not evaluated in display.
  4. I have used exact->inexact because that's what I think your intention is.

以下作品.

(define x (list 10 60 3 55 15 45 40))

(define (sum x)
    (if (null? x)
        0
        (+ (car x) (sum (cdr x)))))

(define (average x)
    (/ (sum x) (length x)))

(display (exact->inexact (average x)) (current-output-port))

这篇关于方案查找列表的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆