scheme

Square roots by Newton’s method

五迷三道 提交于 2020-02-07 02:36:22
问题 The following Scheme program implements Newton’s method for computing the square root of a number: (import (scheme small)) (define (sqrt x) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (define (good-enough? guess) (define tolerance 0.001) (< (abs (- (square guess) x)) tolerance)) (define (improve guess) (if (= guess 0) guess (average guess (/ x guess)))) (define (average x y) (/ (+ x y) 2)) (define initial-guess 1.0) (sqrt-iter initial-guess))

Writing a function that finds the smallest k such that the difference between x and a function

混江龙づ霸主 提交于 2020-02-06 16:59:47
问题 I am having trouble where my code feels incomplete and plain wrong. For my function (terms-needed x tol) I am supposed to find the smallest k such that the difference between x and (square (babylonian x k)) is less than tol (tolerance). In other words we are supposed to measure how large k needs to be in the function (babylonian x k) to provide a good approximation of the square root. As of right now I am getting an error of "application: not a procedure;" with my code (define (square x) (* x

Writing a function that finds the smallest k such that the difference between x and a function

此生再无相见时 提交于 2020-02-06 16:56:21
问题 I am having trouble where my code feels incomplete and plain wrong. For my function (terms-needed x tol) I am supposed to find the smallest k such that the difference between x and (square (babylonian x k)) is less than tol (tolerance). In other words we are supposed to measure how large k needs to be in the function (babylonian x k) to provide a good approximation of the square root. As of right now I am getting an error of "application: not a procedure;" with my code (define (square x) (* x

Writing a function that finds the smallest k such that the difference between x and a function

感情迁移 提交于 2020-02-06 16:56:15
问题 I am having trouble where my code feels incomplete and plain wrong. For my function (terms-needed x tol) I am supposed to find the smallest k such that the difference between x and (square (babylonian x k)) is less than tol (tolerance). In other words we are supposed to measure how large k needs to be in the function (babylonian x k) to provide a good approximation of the square root. As of right now I am getting an error of "application: not a procedure;" with my code (define (square x) (* x

I need help converting between the let form and unnamed procedure form

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-05 04:54:58
问题 im trying to convert from a let-form to an unamed procedure form and i just can't get the hang of it. the let procedure is this. (define max-recursive (lambda (lst) (if (null? (cdr lst)) (car lst) (let ((m0 (car lst)) (m1 (max-recursive (cdr lst)))) (if (> m0 m1) m0 m1 ) ) ))) and what i've done so far is this (define max-recursive (lambda (lst) (if (null? (cdr lst)) (car lst) ((lambda (m0 m1) (if (> m0 m1) m0 m1 ) ) car lst (max-recursive (cdr lst))) ))) any help would be appreciated thank

python爬虫笔记

余生颓废 提交于 2020-02-04 00:54:12
1.urllib库中request,parse的学习 1.1 简单的请求页面获取,并下载到本地 request的使用 from urllib import request # 获取此网页的demout resp = request.urlopen('http://www.baidu.com') # 读出10个字符 # 结果为 b'<!DOCTYPE ' b代表bytes 是一个字节流 # '<!DOCTYPE ' 包括空格 正好十个字符 # print(resp.read(10)) # 读取一行 # 结果为 b'<!DOCTYPE html>\n' # \n 代表换行 # ()里面可以写读几行 # print(resp.readline()) # 全部读取 二者对应的返回类型不同 # <class 'list'> readlines # <class 'bytes'> read # print(type(resp.readlines())) # print(type(resp.read())) # 下载到本地文件夹 request.urlretrieve('http://www.baidu.com', 'baidu.html') 1.2 parse的使用 1.2.1 解决中文与码的对应问题 例:中文变成码 name=%E9%AB%98%E8%BE%BE 这些带%和中文之间的转换 1

Tracking number of function calls + closures (à la SICP) in Python

三世轮回 提交于 2020-02-03 21:50:59
问题 This is a question about scope and closures in Python , motivated by an exercise in SICP. Much thanks for your time if you read this! A question (3.2) in SICP asks one to create a procedure "make-monitored", that takes in a function f (of one parameter) as input and returns a procedure that keeps track of how many times f has been called. (If the input to this new procedure is "num-calls" it returns the number of times f has been called, if it is "reset" it resets counter to 0 and anything

Tracking number of function calls + closures (à la SICP) in Python

爷,独闯天下 提交于 2020-02-03 21:48:06
问题 This is a question about scope and closures in Python , motivated by an exercise in SICP. Much thanks for your time if you read this! A question (3.2) in SICP asks one to create a procedure "make-monitored", that takes in a function f (of one parameter) as input and returns a procedure that keeps track of how many times f has been called. (If the input to this new procedure is "num-calls" it returns the number of times f has been called, if it is "reset" it resets counter to 0 and anything

Trouble understanding / visualising SICP streams Hamming numbers program

て烟熏妆下的殇ゞ 提交于 2020-02-03 20:54:27
问题 I'm basically stuck at excercise 3.56 in SICP. The problem goes like this: Exercise 3.56. A famous problem, first raised by R. Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2, 3, or 5. One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2, 3, and 5. But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As

Can Scheme expand a list as arguments?

ⅰ亾dé卋堺 提交于 2020-01-30 04:43:46
问题 Considered that I have a procedure (plus x y) witch takes exactly two args. And now I also have a list which contains two objects like (list 1 2) . So, if there's any magic way to expand the list as two arguments. We have a dot notion version, but that isn't what i want. I just want to expand the list to make Scheme believe I passed two arguments instead of a list. Hope those Ruby codes help: a = [1, 2] def plus(x,y); x+y; end plus(*a) # See that a is an array and the plus method requires #