首页 > 其他分享 >sicp每日一题[2.34]

sicp每日一题[2.34]

时间:2024-09-30 17:02:39浏览次数:6  
标签:coefficient 2.34 terms sequence 每日 sicp horner eval polynomial

Exercise 2.34

Evaluating a polynomial in x at a given value of x can be formulated as an accumulation. We evaluate the polynomial

an x^n + a{n-1} x^(n-1) + ... + a1 x + a0

using a well-known algorithm called Horner's rule, which structures the computation as

(...(an x + a{n-1})x + ... + a1)x + a_0.

In other words, we start with an, multiply by x, add a{n−1}, multiply by x, and so on, until we reach a0.

Fill in the following template to produce a procedure that evaluates a polynomial using Horner's rule. Assume that the coefficients of the polynomial are arranged in a sequence, from a0 through an.

(define (horner-eval x coefficient-sequence)
  (accumulate (lambda (this-coeff higher-terms) <??>)
              0
              coefficient-sequence))

For example, to compute 1 + 3x + 5x^3 + x^5 at x = 2 you would evaluate

(horner-eval 2 (list 1 3 0 5 0 1))

这道题挺简单的,主要是题目里给的参数名起的太好了,本来我还不知道咋写的,一看 this-coeff 和 higher-terms 这俩参数,瞬间就明白了。

(define (horner-eval x coefficient-sequence)
  (accumulate (lambda (this-coeff higher-terms) (+ (* higher-terms x) this-coeff))
              0
              coefficient-sequence))


(horner-eval 2 (list 1 3 0 5 0 1))

; 执行结果
79

标签:coefficient,2.34,terms,sequence,每日,sicp,horner,eval,polynomial
From: https://www.cnblogs.com/think2times/p/18442165

相关文章

  • 9.29每日总结
    今天做完了“四则运算”和“生成验证码”,其中“生成验证码”这道题暑假的时候跟着网课做过初级版的,今天又加以改进了不少,为此把黑马的字符串章节差不多看完了,收获比较大的除了StringBuilder和StringJoiner之外,就是“验证码”这道题中用到的字符串转为字符数组(toCharArray),而“四......
  • 洛谷每日一题(P2580 于是他错误的点名开始了)字典树/哈希表
    原题目链接:P2580于是他错误的点名开始了-洛谷|计算机科学教育新生态(luogu.com.cn)原题目截图:思路分析:解法一:哈希表法显而易见的一种思路,我们不妨模拟一下:当教练每次点名,我作为特派员,便查看一下有没有这个学生,是不是点过了这个学生。我们查看的过程,就依赖于一张表......
  • 洛谷每日一题(P1481 魔族密码)字典树解法
    原题目链接:P1481魔族密码-洛谷|计算机科学教育新生态(luogu.com.cn)原题目截图:思路分析:这道题的话其实有很多种方法,可以用动态规划做,不过我一看到这道题,脑子里不禁蹦出一个数据结构:“字典树”!字典树+深度优先搜索。那么在这之前,我们先来了解一下什么是字典树吧!什......
  • sicp每日一题[2.33]
    Exercise2.33Fillinthemissingexpressionstocompletethefollowingdefinitionsofsomebasiclist-manipulationoperationsasaccumulations:;p表示一个函数,sequence表示一个列表;这个函数将对列表中每一个元素进行p操作(define(mappsequence)(accu......
  • 【力扣 | SQL题 | 每日三题】力扣1068, 1204, 1193, 1084, 1141
    1.力扣1068:产品销售分析11.1题目:销售表 Sales:+-------------+-------+|ColumnName|Type|+-------------+-------+|sale_id|int||product_id|int||year|int||quantity|int||price|int|+-------------+-......
  • sicp每日一题[2.31]
    Exercise2.31AbstractyouranswertoExercise2.30toproduceaprocedure$tree-map$withthepropertythat$square-tree$couldbedefinedas(define(square-treetree)(tree-mapsquaretree))这道题跟上面一道的map实现几乎一模一样,我还以为我理解错题目......
  • sicp每日一题[2.32]
    上一道题没什么改动,再来一道Exercise2.32Wecanrepresentasetasalistofdistinctelements,andwecanrepresentthesetofallsubsetsofthesetasalistoflists.Forexample,ifthesetis(123),thenthesetofallsubsetsis(()(3)(2)(23)......
  • GitHub每日最火火火项目(9.27)
    项目名称:localsend/localsend项目介绍:“localsend/localsend”是一个极具价值的开源项目。它为用户提供了一种跨平台的文件传输替代方案,可媲美AirDrop。在当今数字化时代,人们常常需要在不同操作系统的设备之间传输文件,但并非所有设备都能使用AirDrop。这个项目的出......
  • 9.24每日总结
    微服务拆分作业参考依赖user-service的pom.xml文件内容如下:<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xs......
  • 9.26每日总结
    给出SpringbootCloud的server:port:8084spring:application:name:user-serviceprofiles:active:devdatasource:url:jdbc:mysql://${hm.db.host}:3306/hm-user?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=......