Error of calling functions in Scheme - function

I am trying to define two functions using Scheme. One is the close_to function which looks like this:
(define (close_to x y)(if(< (abs (- x y)) 0.0001)(#t)#f))
it should return true if the number x and y has a difference that is smaller than 0.0001 and false otherwise. However, it keeps throwing the error:
function call: expected a function after the open parenthesis, but received true
when I call it
(close_to 4 3.99999999)
The second function is the improve function which looks like this:
(define (improve x y)(average y /(x y)))
it should return the average of y and x/y. Similarly, I am getting the error:
function call: expected a function after the open parenthesis, but received 1
when I call it
(improve 1 2)
What am I doing wrong? Can anyone help me?

Your problem is the (#t) and (#f).
#t is not a function. Rather, rewrite the first one like this:
(define (close_to x y)
(if (< (abs (- x y)) 0.0001)
#t
#f))
The other is an exercise for the reader.

Related

Why is cond function not printing in scheme?

I am very new to scheme, and I am having trouble getting simple cond functions that I make to print in DrRacket ide. When I run these two functions:
(define (test x)
(cond
[(zero? x) (error "doesn't get here, either")]
[(positive? x) 'here]))
(define (compare x y)
(cond [(equal? x y) "Is Equal"]))
it prints:
> test 12
#<procedure:test>
12
> compare 12 12
#<procedure:compare>
12
12
Why will it not output any of the errors, or "Is Equal"? It works fine if I run the cond statements directly and replace the variables.
You're not actually calling the new procedures, you must surround the procedure name and its arguments between brackets (), just as you did with all the other procedures you're using in your solution! This is the way:
(test 12)
=> 'here
(compare 12 12)
=> "Is Equal"

Scheme (DrRacket) lambda expression

I do not understand the following program code. Can anyone explain it to me?
(define myFunction
(lambda (f x y z)
(f x y z)))
How can I simplify it?
You can't simplify it much, except for making it look a bit syntactic, as
(define (myFunction f x y z)
(f x y z))
which is a syntactic shortcut for defining the exact same thing.
This way, you can treat it mentally as a rewriting rule: whenever you see
(myFunction f x y z)
in your code - whatever those f, x, y and z placeholders are referring to - you can replace it with
(f x y z)
while substituting the actual values, a.k.a. arguments, for the placeholders, a.k.a. function parameters.
Thus you see that for the new code to make sense, f should be a function capable of accepting three arguments, x, y and z.
So for example, if you define (define (f x y z) (+ x y z)), you can call
(myFunction f 1 2 3)
and get a result back - which is the result of calling the function f you've just defined above, with the values 1, 2 and 3.
f inside myFunction will refer to the global name f which refers to a value - a function named f you've defined. The lambda form defines a value - an anonymous function - and define binds a name f to it, so that any use of that name refers to that value, from now on. In Scheme, functions are values like any other.
That global function f is defined to apply + to the three arguments it receives, when it receives them. It in effect is saying, "give me some three values and I'll sum them up for you", and the call (myFunction f 1 2 3) supplies it with the three values of your choosing.
lambda ... creates a function which expects 4 arguments, the first of which (f) should itself be a function. (f x y z) applies this f to the other 3 arguments.
define gives the name myFunction to the lambda.

Flip function in Scheme

does anybody know how can I create a function in Scheme that takes no arguments and everytime I call it returns 0 or 1, depending on how many times it's been called? For example, the 1st time returns 1, the 2nd 0, the 3rd 1, etc.
I suppose I have to use a local variable inside the function, but I don't know exactly how, so that it changes value everytime I call it.
You didn't say how you are calling your function. Did you define some named function as the lambda you return with you make-flip function? I guess this is "making closures 101" but it's the first time I've done to my recollection. Anyway, I tried this way and it seemed to work:
(define (make-flipper)
(let ((flip 0))
(lambda ()
(set! flip (if (= flip 0) 1 0))
flip)))
(define doit (make-flipper))
(doit)
(doit)
(doit)
--results in 1, then 0, then 1. I guess you could change the value in the let if you want it to start with 0.
Your code doesn't work because you're overusing parentheses, which makes your code try to call "procedures" that aren't procedures.
Your code,
(define (make-flip)
(let ((x 1))
(lambda ()
((set! x (- 1 x))
(if (= x 0) (1) (0))))))
attempts the procedure calls (0) and (1), and it also tries to "call" the result of the sequence
(set! x (- 1 x))
(if (= x 0) (1) (0)))
(Scheme never ignores any parentheses the way some other languages do.)
If you fix those,
(define (make-flip)
(let ((x 1))
(lambda ()
(set! x (- 1 x))
(if (= x 0) 1 0))))
it works.
The conditional isn't necessary though, you can also say
(define (make-flip)
(let ((x 0))
(lambda ()
(set! x (- 1 x))
x)))
and get the same result.
If you only need one procedure of this kind, you can also do as follows:
(define flip
(let ((x 1))
(lambda ()
(begin0
x
(set! x (- 1 x))))))
Testing:
> (flip)
1
> (flip)
0
> (flip)
1
> (flip)
0

Defining a Racket Function?

I'm supposed to define the function n! (N-Factorial). The thing is I don't know how to.
Here is what I have so far, can someone please help with this? I don't understand the conditionals in Racket, so an explanation would be great!
(define fact (lambda (n) (if (> n 0)) (* n < n)))
You'll have to take a good look at the documentation first, this is a very simple example but you have to understand the basics before attempting a solution, and make sure you know how to write a recursive procedure. Some comments:
(define fact
(lambda (n)
(if (> n 0)
; a conditional must have two parts:
; where is the consequent? here goes the advance of the recursion
; where is the alternative? here goes the base case of the recursion
)
(* n < n))) ; this line is outside the conditional, and it's just wrong
Notice that the last expression is incorrect, I don't know what it's supposed to do, but as it is it'll raise an error. Delete it, and concentrate on writing the body of the conditional.
The trick with scheme (or lisp) is to understand each little bit between each set of brackets as you build them up into more complex forms.
So lets start with the conditionals. if takes 3 arguments. It evaluates the first, and if that's true, if returns the second, and if the first argument is false it returns the third.
(if #t "some value" "some other value") ; => "some value"
(if #f "some value" "some other value") ; => "some other value"
(if (<= 1 0) "done" "go again") ; => "go again"
cond would work too - you can read the racket introduction to conditionals here: http://docs.racket-lang.org/guide/syntax-overview.html#%28part._.Conditionals_with_if__and__or__and_cond%29
You can define functions in two different ways. You're using the anonymous function approach, which is fine, but you don't need a lambda in this case, so the simpler syntax is:
(define (function-name arguments) result)
For example:
(define (previous-number n)
(- n 1))
(previous-number 3) ; => 2
Using a lambda like you have achieves the same thing, using different syntax (don't worry about any other differences for now):
(define previous-number* (lambda (n) (- n 1)))
(previous-number* 3) ; => 2
By the way - that '*' is just another character in that name, nothing special (see http://docs.racket-lang.org/guide/syntax-overview.html#%28part._.Identifiers%29). A '!' at the end of a function name often means that that function has side effects, but n! is a fine name for your function in this case.
So lets go back to your original question and put the function definition and conditional together. We'll use the "recurrence relation" from the wiki definition because it makes for a nice recursive function: If n is less than 1, then the factorial is 1. Otherwise, the factorial is n times the factorial of one less than n. In code, that looks like:
(define (n! n)
(if (<= n 1) ; If n is less than 1,
1 ; then the factorial is 1
(* n (n! (- n 1))))) ; Otherwise, the factorial is n times the factorial of one less than n.
That last clause is a little denser than I'd like, so lets just work though it down for n = 2:
(define n 2)
(* n (n! (- n 1)))
; =>
(* 2 (n! (- 2 1)))
(* 2 (n! 1))
(* 2 1)
2
Also, if you're using Racket, it's really easy to confirm that it's working as we expect:
(check-expect (n! 0) 1)
(check-expect (n! 1) 1)
(check-expect (n! 20) 2432902008176640000)

how to return function in elisp

This is related to this question: elisp functions as parameters and as return value
(defun avg-damp (n)
'(lambda(x) (/ n 2.0)))
Either
(funcall (avg-damp 6) 10)
or
((avg-damp 6) 10)
They gave errors of Symbol's value as variable is void: n and eval: Invalid function: (avg-damp 6) respectively.
The reason the first form does not work is that n is bound dynamically, not lexically:
(defun avg-damp (n)
(lexical-let ((n n))
(lambda(x) (/ x n))))
(funcall (avg-damp 3) 12)
==> 4
The reason the second form does not work is that Emacs Lisp is, like Common Lisp, a "lisp-2", not a "lisp-1"