If you have not already done so, please take the quiz before continuing with lab.
Instructions for students & labbies: Students use DrScheme, following the design recipe, on the exercises at their own pace, while labbies wander among the students, answering questions, bringing the more important ones to the lab's attention. There are more exercises here than most students will be able to do during lab, so feel free to skip the many challenge problems.
Reminder: While we've looked at how to define filter,
map, foldr, etc., many of these are also
built-in to DrScheme. Feel free to use them.
lambda
Let's start with some review.
(lambda (x …) body)
creates a function, just like
(define (f x …) body).
However, the latter form also names the function.
In fact, we can separate the naming and definition of a function:
(define (f x y) (+ x y))
=
(define f (lambda (x y) (+ x y)))
Neither form is better style, but you should understand this equivalence.
|
It doesn't seem possible to define a recursive function without giving it a name. But you can! However, it's quite tricky. See COMP 311 for details.
Derivatives
Last lab focused on abstracting list-processing functions. But, the ideas of abstractions are not limited to any one type of data or operation. This week, we'll focus more on common mathematical functions.
One common mathematical problem is to find the slop of some function f and some point x. Given f, its derivative f′ is a function such that f′(x) is the slope of f at x. Finding the derivative of a function exactly in symbolic form is somewhat difficult, and is part of calculus. Finding a numeric approximation of the derivative is much simpler, and what we'll do.
Consider the following picture:
![]()
Given f and x, the value of the derivative can be
approximated as the slope between
f(x-eps) and
f(x+eps), assuming
eps is a small value. I.e., we can use the equation
|
Taylor Series
Read sections 23.1 and 23.3 of the online textbook. The code you'll use in lab is provided below and is a slight modification of that in the book.
; series : natural (natural -> number) -> number
; Returns the sum of the first n numbers in the sequence a-term, i.e.,
; (+ (a-term 0) … (a-term (sub1 n))).
(define (series n a-term)
(cond
[(= n 0) (a-term n)]
[else (+ (a-term n) (series (- n 1) a-term))]))
; e-power : natural -> number
; Returns the x-th power of the constant e, as approximated by n terms of
; a Taylor series.
(define (e-power x n)
(local ((define (e-taylor i)
(exact->inexact (/ (expt x i) (! i))))
(define (! n)
(cond
[(= n 0) 1]
[else (* n (! (sub1 n)))])))
(series n e-taylor)))
|
If we end up wanting to define lots of Taylor series, we might prefer
to have an abstraction specifically "tailored" for Taylor series,
similar to series.
The difference is that to describe each term, we provide a function
of two arguments, a number x and index i.
Of course, we still need to provide an x sometime, so the
result of the function is itself a function of x.
; taylor-series : natural (number natural -> number) -> (number -> number)
; Returns a function to compute the sum of the first n numbers in the
; sequence a-term, i.e.,
; (+ (a-term x 0) … (a-term x (sub1 n))).
(define (taylor-series n a-term)
…)
The answers to both of these are fairly simple.
The difficulty is in the thought process.
|