I'm trying to call a subroutine from a function on scheme this what i have so far.
(define (main)
(display "Ingrese un parametro: ")
(define x (read-line))
(newline)
(display "Ingrese una posicion: ")
(define dig (read))
(newline)
(if (string? x)
(begin
(if (number? dig)
(begin
;(vababa x dig))) <---calling subrutine
this is the subroutine that im trying to call
define (vababa x1 dig1)
(define a (string-pad-right x dig1))
(define b (string-pad-left x dig1))
(define z (string-append a b))
z
)
but its not returning nothing.. please tell me what im doing wrong.
In vababa you are using a unbound variable x. Perhaps you meant x1?
It's not allowed to use define in procedure bodies that evaluates to values based on previous ones.
(define (vababa x1 dig1)
(define a (string-pad-right x dig1)) ; x does not exist anywhere
(define b (string-pad-left x dig1)) ; x does not exist anywhere
(define z (string-append a b)) ; a and b does not exist yet
z) ; now a and b exists but you won't get there since `string-append` didn't get string arguments
You can do one of:
(define (vababa x1 dig1)
(string-append (string-pad-right x1 dig1) ; replaced x with x1
(string-pad-left x1 dig1))) ; replaced x with x1
(define (vababa x1 dig1)
(define a (string-pad-right x1 dig1)) ; replaced x with x1
(define b (string-pad-left x1 dig1)) ; replaced x with x1
;; in the body a and b exists
(string-append a b))
(define (vababa x1 dig1)
(let ((a (string-pad-right x1 dig1)) ; replaced x with x1
(b (string-pad-left x1 dig1))) ; replaced x with x1
(string-append a b)))
(define (vababa x1 dig1)
(let* ((a (string-pad-right x1 dig1)) ; replaced x with x1
(b (string-pad-left x1 dig1)) ; replaced x with x1
(z (string-append a b)))
z))
Related
I am using the interpreter from browser (without any local setup): https://inst.eecs.berkeley.edu/~cs61a/fa14/assets/interpreter/scheme.html
and getting the following interpreter exception message:
SchemeError: unknown identifier: and
Current Eval Stack:
-------------------------
0: and
1: (cond (and (< x y) (< x z)) (sqrt-sum y z))
2: (f 1 2 3)
for the following code:
; define a procedure that takes three numbers
; as arguments and returns the sum of the squares
; of the two larger numbers
(define (square) (* x x))
(define (sqrt-sum x y)
(+ (square x) (square y)))
(define (f x y z)
(cond (and (< x y) (< x z)) (sqrt-sum y z))
(cond (and (< y x) (< y z)) (sqrt-sum x z))
(cond (and (< z y) (< z x)) (sqrt-sum x y)))
(f 1 2 3)
I am struggling to find any info about specific Scheme version this interpreter is based on; sorry
That's not the correct syntax for cond. The syntax is
(cond (condition1 value1...)
(condition2 value2...)
...)
In your code the first condition should be the expression (and (< x y) (< x z)). But you don't have the parentheses around the condition and value. You have just and where the condition should be, not (and (< x y) (< x z)). Since and isn't a variable with a value, you get an error because of that.
The correct syntax is:
(define (f x y z)
(cond ((and (< x y) (< x z)) (sqrt-sum y z))
((and (< y x) (< y z)) (sqrt-sum x z))
((and (< z y) (< z x)) (sqrt-sum x y))))
According to Newton-Raphson's:
Xn+1 = Xn - f(Xn) / f'(Xn)
(newtonRhap x f fx)
(newtonRhap 0.1 sin cos) => 0
(newtonRhap 2.0
(lambda (x) (- (* x x) x 6)) ; f
(lambda (x) (- (* 2 x) 1 )) ) => 3 ; f'
How can I implement this function?
The routine will stop iteration when the change in solution is less than pre-set tolerance.
I use a global variable for it, (define TOL 1e-6):
#lang racket
(define TOL 1e-6)
(define (func f x) (f x))
(define (f x) (- (* x x) x 6))
(define (fx x) (- (* 2 x) 1))
(define x 2.0)
(define (newtonRhap x ff ffx)
( (> (- x (/ ff ffx)) TOL)
(newtonRhap (- x (/ ff ffx)) ff ffx)
(list x) ) )
(display (newtonRhap x (f x) (fx x)) )
Good attempt, you need to modify newtonRaph function to use IF though, like this
(define (newtonRhap x ff ffx)
(if (> (- x (/ ff ffx)) TOL)
(newtonRhap (- x (/ ff ffx)) ff ffx))
(list x)))
First, you're missing an if there:
(define (newtonRhap x ff ffx)
( if (> (- x (/ ff ffx)) TOL)
(newtonRhap (- x (/ ff ffx)) ff ffx)
(list x) ) )
Without it you would be calling the result of the Boolean test as a function with the following two forms values as arguments (it could work in a lazy functional language with Church-encoded conditionals, but that's besides the point, and Scheme / Racket is not lazy).
Second, and even more crucially, the last two arguments to newtonRhap are functions, but you're treating them as if they were numeric values. Correcting this gives us
(define (nl x) ; for debugging insight;
(newline) ; for production redefine it as
(display x) ; (define (nl x) x)
x)
(define (newtonRhap x ff ffx)
(let ((new-x (nl (- x (/ (ff x) (ffx x)))))) ; Follow The Formula
( if (> (abs (- x new-x)) TOL) ; use abs
(newtonRhap new-x ff ffx) ; make next step
(list new-x) ) ) ) ; return the new x, it's closer to the true result
Now we can run
(display (newtonRhap x f fx))
3.333333333333333
3.019607843137255
3.0000762951094835
3.000000001164153
3.0(3.0)
>
. I am trying to teach my self some computer science skills independently. The problem I am working on wants me to create a way to choose the two biggest numbers out of three then find the sum of squares for the two numbers.
(define (pro x y z)
(cond( (and (< x y) (< x z)) (define a y)(define b z))
( (and(< y x) (< y z)) (define a x) (define b z))
( else ((define a x )(define b y))))
(+ (* a a) (* b b))
When I run the function with z being the smallest or tied for the smallest number I get the following error:
Error: #<undef> is not a function [pro, (anon)]
Why am I getting this error and how do I fix it?
I have been using repl.it to run this program, if that matters.
First, using define's that way is totally bizarre for Scheme code.
After that, I see two problems with the code. The first, the one that's creating the error you're getting, is that you have an extra layer of parens in the else clause. The following
((define a x) (define b y))
is going to evaluate the first define and try to apply it as a procedure. The evaluation of (define ...) returns the #undef which is the source of your error messsage.
If you fixed that problem, your next problem is that your sum of squares code is outside the scope of the defines in your cond and you'll find that a and b are not defined out there.
You should do something like this:
(define (max-of-3 x y z)
(cond
((and (< x y) (< x z)) (values y z))
((and (< y x) (< y z)) (values x z))
(else (values x y))))
(define (pro x y z)
(let-values (((a b) (max-of-3 x y z)))
(+ (* a a) (* b b))))
or even
(define (pro x y z)
(let-values
(((a b) (cond
((and (< x y) (< x z)) (values y z))
((and (< y x) (< y z)) (values x z))
(else (values x y)))))
(+ (* a a) (* b b))))
I am newbie to Scheme programming and trying to define a var in if condition. For example, I have:
(if (< x y) (define x y) ) ;(GOAL: if x < y, than x=y..)
But I got the error:
let: bad syntax (not an identifier and expression for a binding) in:...
Any ideas how to resolve this, would be greatly appreciated.
p.s. Sorry for my English
Unlike imperative languages you should refrain not use define or set! to update variables where you can avoid it. In some circumstances it's needed, like in generators.
Since you don't have a full code example where you'd like to do this I cannot see what obvious solution is to be used.
The way to store intermediate values if by let or recursion:
;; within the let block x shadows the original x
;; with the smalles of the outer x and y
(let ((x (if (< x y) x y)))
(do-something x))
You can do several intermediates by let*
(let* ((tmp (+ x y))
(tmp2 (* tmp y))) ; tmp is bound here
(do-something-with tmp2)); or tmp and tmp2
You you can use recursion, where you update cur and lst in th innner procedure by recursion:
(define (mmin x . xs)
(define (min-aux cur lst)
(cond ((null? lst) cur)
((<= cur (car lst)) (min-aux cur (cdr lst)))
(else (min-aux (car lst) (cdr lst)))))
(min-aux x xs)) ; start recursion
It is an error to define something that is already defined so thats why I defined
If you need to do this top level you can:
(define min_xy (if (< x y) x y))
min_xy. To alter a binding destructively (get it to reference another value) you can use set!
(set! x (+ x 1)) ; increases x
You'll alter the most local definition and it's an error if it doesnæt already exist. This can be used for creating generators:
(define (generator start alter-proc)
(lambda () ; returns a procedure taking 0 arguments
(let ((old start)) ; temporary store what start points to
(set! start (alter-proc start)) ; change what the local start points to
old))) ; return the old value
(define counter (generator 1 (lambda (x) (+ x 1))))
(counter) ; ==> 1
(counter) ; ==> 2
(define doubler (generator 1 (lambda (x) (* x 2))))
(doubler) ; ==> 1
(doubler) ; ==> 2
(doubler) ; ==> 4
Using define is wrong; you are not defining a function here. There are two solutions:
(if (< x y) (set! x y) (void)) ; if x < y set x = y; else do nothing
Or
(set! x (if (< x y) y x)) ; sets x to y if (x<y) is true; else sets x to x
I have to remove every lambda from the following code, and I can't use other functions in the global space. (((f 1) 2) 3) should produce 6.
(define f (lambda (x)
(lambda (y)
(lambda (z)
(+ x y z)))))
I have tried using define in define, but the problem is with the (((f 1) 2) 3) having to give 6. I dont see how I can use the 2 and 3 inside function f, if they are given outside the function? It is OK if the lambdas are “under the hood,” they just have to not be visible.
Try
(define (f x)
(define (g y)
(define (h z)
(+ x y z))
h)
g)
or
(define (((f x) y) z)
(+ x y z))