Adding the result of computation as an argument in Scheme - scheme

I try to do a program that calculates the square of a number in Scheme but it gives me as an error that I gave the abs procedure two arguments instead of one. This is my first program in Scheme and have no clue how to do it. I tried to change the position of the brackets but it doesn't work.
(define (square X)
(try 1 X))
(define (try guess X)
(if (good-enaugh? guess X)
guess
((try improve guess X) X)))
(define (good-enaugh? guess X)
(< (abs(- (* guess guess) X)) 0.0001))
(define (improve guess X)
(/(+ (/ X guess) guess) 2))

You are almost there. The problem is that the second branch of your conditional in try has a pair of parentheses too much.
(define (try guess X)
(if (good-enaugh? guess X)
guess
((try improve guess X) X)))
Should become:
(define (try guess X)
(if (good-enaugh? guess X)
guess
(try (improve guess X) X)))
You make a call to the function try. The first parameter is the result of (improve guess X) and the second argument is the number X. Notice that I also changed the parenthesis around the call to improve. The way you wrote it was calling the function try with as first argument the function improve, second argument guess and third argument X.
Full source
For good measure the complete source is listed below.
(define (square X)
(try 1 X))
(define (try guess X)
(if (good-enaugh? guess X)
guess
(try (improve guess X) X)))
(define (good-enaugh? guess X)
(< (abs(- (* guess guess) X)) 0.0001))
(define (improve guess X)
(/(+ (/ X guess) guess) 2))
> (square 13)
3,....
Link to repl.it

Related

SICP Exercise 1.8 - Why doesn't my function terminate?

I'm trying to implement Newton's method for cube roots, but my function seems to hang. My code is almost identical to the code the book shows for the square root, with the improve function modified as appropriate.
Here is my code...
(define (cube-root x)
(cube-root-iter 1.0 x))
(define (cube-root-iter guess x)
(if (good-enough? guess x)
guess
(cube-root-iter (improve x guess) x)
)
)
(define (good-enough? guess x)
(< (abs (- (* guess guess) x)) 0.001))
(define (improve x guess)
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3)
)
I tested improve and good-enough? independently, and they seem to give the right answers. Anyone know why my code doesn't terminate?
I'm doing this in Racket with the sicp package in case that makes any difference.
You want to compute the cube root. The cube root of x means a value y for which y*y*y = x, by definition. You try to approximate y using guess, but you compute only y^2. Need to multiply it 3 times:
(define (good-enough? guess x)
(< (abs (- (* guess guess guess) x))
0.001))
If you multiply it only 2 times, y^2 will never get close enough to y^3, which gets closer to x. If you try to compute cube root of 1 or of 0 with your code it might work however (and only for these inputs it might work).

Racket: Trying to subtract numbers, getting list

I'm currently learning Racket/Scheme for a course (I'm not sure what's the difference, actually, and I'm not sure if the course covered that). I'm trying a basic example, implementing the Newton method to find a square root of a number; however, I ran into a problem with finding the distance between two numbers.
It seems that for whatever reason, when I'm trying to apply the subtraction operator between two numbers, it returns a list instead.
#lang racket
(define distance
(lambda (x y) (
(print (real? x))
(print (real? y))
(abs (- x y))
)
)
)
(define abs
(lambda x (
(print (list? x))
(if (< x 0) (- x) x)
)
)
)
(distance 2 5)
As you can see, I've added printing of the types of variables to make sure the problem is what I think it is, and the output of all those prints is #t. So:
In calling distance, x and y are both real.
In calling abs, x is a list.
So, the conclusion is that (- x y) returns a list, but why?
I double-checked with the documentation and it seems I'm using the subtraction operator correctly; I've typed (- 2 5) and then (real? (- 2 5)) into the same REPL I'm using to debug my program (Dr. Racket, to be specific), and I'm getting the expected results (-3 and #t, respectively).
Is there any wizard here that can tell me what kind of sorcery is this?
Thanks in advance!
How about this...
(define distance
(lambda (x y)
(print (real? x))
(print (real? y))
(abs (- x y))))
(define abs
(lambda (x) ;; instead of (lambda x ...), we are using (lambda (x) ...) form which is more strict in binding with formals
(print (list? x))
(if (< x 0) (- x) x)))
Read further about various lambda forms and their binding with formals.

Nested function in programing

I am reading Newton's method in SICP, where he introduce the block structure which have the function nested, say
(define (sqrt x)
(define (good enough ? guess x)
(<(abs(-(square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter(improve guess x)x)))
(sqrt-iter 1.0 x))
I am quite confused because in my understanding of nested, "good enough ?" and "improve guess" should be nested
in the body of "sqrt-iter", and "sqrt iter" should be put in the body of "sqrt x"
(define (sqrt x)
(sqrt-iter 1.0 x
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter(improve guess x)x)
(define (good enough ? guess x)
(<(abs(-(square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
))
))
why it is not like that.I am using LISP
Notice that sqrt-iter is already defined inside the body of sqrt. But the way you wrote it doesn't make sense, you're trying to call sqrt-iter but you're defining it inside the call as a third parameter. This isn't right:
(sqrt-iter 1.0 x (define (sqrt-iter ...)))
You must define it before calling it, and you can't define it as if it were a parameter, as part of the procedure call. This is the correct way:
(define (sqrt-iter guess x) ...) ; first define
(sqrt-iter 1.0 x) ; after defining, you can call it
Your other suggestion makes more sense, it'd be possible to define good-enough? and improve inside sqrt-iter, but maybe it's clearer in the way they wrote it in the book. This is valid, but has the disadvantage that good-enough? and improve will be redefined each time sqrt-iter is called:
(define (sqrt x)
(define (sqrt-iter guess x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)x)))
(sqrt-iter 1.0 x))
You should read more carefully the section of the book that explains how procedures are defined and used in Scheme, you seem to be confusing concepts, mixing parameters, procedure definitions and procedure invocations.

Dr racket define error in student language. define: expected only one expression for the function body, but found 3 extra parts

When I write code in Dr Racket, I got error message
unsaved-editor:8:2: define: expected only one expression for the
function body, but found 3 extra parts in: (define (improve guess x)
(average guess (/ x guess)))
But this code can run in Racket or repl.it.
I want to know why error is happening in Dr Racket and is my code really wrong?
My code is this:
(define (average x y) (/ (+ x y) 2))
(define (square x) (* x x))
(define (sqrt1 x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(sqrt-iter 1.0 x))
(sqrt1 9)
Your code is OK for Scheme/Racket. However Student Language is a subset of Scheme, highly limited so it's easier for beginners. It's also used in How To Design Programs book. You can read more about Student Languages (actually there is five of them) on https://docs.racket-lang.org/htdp-langs/index.html.
In case of define there are important limitations:
You can have only one expression in the function body.
In function body you can only use expressions, not definitions (so no define inside define).
To make your code valid for Student Language, depending on Level (Beginner, Intermediate etc), you can:
use letrec* or local instead of define for all local definitions
or
define good-enough, improve and sqrt-iter as top level functions.

Internal Definitions in Block Structure

(define (sqrt x)
  (define (good-enough? guess x)
    (< (abs (- (square guess) x)) 0.001))
  (define (improve guess x)
    (average guess (/ x guess)))
  (define (sqrt-iter guess x)
    (if (good-enough? guess x)
        guess
        (sqrt-iter (improve guess x) x)))
  (sqrt-iter 1.0 x))
I can't seem to wrap my head around around internal block structure. If the syntax for define is: (define procedure arg arg body). How are you able to define all the other local-scope variables inside the top-level define?
Is there a syntax exception for defines?
The syntax for define is not (define procedure arg .. body). It is either (define (name . args) defines ... body1 bodyn ...) which is a shortcut for (define name (lambda args defines ... body1 bodyn ...)). Note that x ... means zero or more so (lambda (a b) (+ a b)) is fine but (lambda (a b) (define (test) (+ a b))) isn't.
Internal define is handled by the lambda syntax. Basically it gets rewritten to a letrec. So
(define (sqrt x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(sqrt-iter 1.0 x))
Becomes:
(define sqrt
(lambda (x)
(letrec ((good-enough? (lambda (guess x)
(< (abs (- (square guess) x)) 0.001)))
(improve (lambda (guess x)
(average guess (/ x guess))))
(sqrt-iter (lambda (guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))))
(sqrt-iter 1.0 x))))
Obviously the reason for using local define is to keep it flatter and more readable than letrec. Keeping the same name even though they are totally different beasts that are handled by different parts of the implementation is a simplification for scheme programmers but harder to grasp and understand if you try to figure out how scheme implementations work.

Resources