I am trying to understand how the count-even function given in my textbook works and I'm not sure if I completely understand it.
I have provided my understanding of the function in comments next to code. Can someone explain what is happening and correct me in each step of this function.
And I don't understand how this function is recursive. What is the base case ?
(define (ce x) ; x is an argument that takes in a list
(if (pair? x) ; I don't understand why this is here
(+ (ce (car x)) ; Add the resuls of recursive call together (0 or 1)
(ce (cdr x)))
(if (and (number? x) (even? x)) ; if x is a number and x is even then return 1 ? Else return 0 ?
1
0) ) )
In a recursive function there are usually two cases:
The result is a call to the same function
The result is a simple value, the base case
You choose between the two cases with an if.
Hence the base case is:
(if (and (number? x) (even? x))
1
0) ) )
And the recursive case is:
(+ (count-evens (car x))
(count-evens (cdr x)))
Comments:
The argument x doesn't need to be a list. pairs? tests for a list. If it is just a value then we have the base case. If an even number then result is one, else zero.
If the argument x is a list, we split it into two parts, the head (car) and the tail (cdr). The head is just a value, and so when we rerun count-evens on it we end up with the base case.
The tail is passed to count-evens which keeps slicing off a value at a time until we have a value in the car and empty list in cdr. In the base case, the first value is assessed for an even number, and the empty list is always evaluated as zero.
Related
I'm having trouble figuring out how to go about creating a function that can take a series of the same function as arguments with the last argument as an operand. For example:
(func sqrt sqrt sqrt 390625)
The call above should return 5 as (sqrt 390625) > (sqrt 625) > (sqrt 25) > 5
I'm having trouble figuring out the exact way I should write this as any way I have tried has given me errors or achieved an infinite loop.
This the code is have so far:
(define func
(lambda L
(cond ( (equal? (length L) 2) ((car L) (cadr L)) ) ;; If the list consists of only 2 elements, carry out the function (element 1) onto the operand (element 2)
( #t (apply (car L) (func (cdr L))) ) ;; otherwise, apply the function (1st element) onto the rest of the list
)
)
)
The first condition works, for example returning 5 if i call (func sqrt 25), however the recursive call is throwing errors.
I would appreciate any help with this.
The OP doesn't provide a definition for chain, so that part is unclear, but I think that a fundamental problem here is that there is no recursive call to func; further, apply isn't used in the right position.
Instead of using (equal (length L) 2) as a base case, it might be nicer to make recursive calls as long as the first element in the input is a procedure, or otherwise just return the element:
#lang racket
(define multi-call
(lambda args
(let ((arg (car args)))
(if (procedure? arg)
(arg (apply multi-call (cdr args)))
arg))))
Here, when arg is a procedure, then it is applied to the result of calling multi-call recursively on the remaining arguments. Note that multi-call takes an arbitrary number of arguments, wrapping them in the list args. The reduction step provides (cdr args), which is a list of the remaining arguments. This means that apply should be used to call multi-call on those remaining arguments because multi-call expects an arbitrary number of arguments, not a list of arguments.
multi-call.rkt> (multi-call sqrt sqrt sqrt 390625)
5
New to common lisp and having a very rookie problem. My function of one variable is supposed to return the absolute of of the entered variable. It works for when the variable is above or equal to 0 but not below, I suspect this is due to the cond function but i'm not sure.
I have tried the code with brackets and without but cannot see why it is failing. I know this is not the best way of solving this problem but i am just trying to get used to the cond statement at this stage.
(defun abs-x (x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (-x))))
The error message when a variable below 0 is entered is '-X is undefined.
Use
(- x)
; ^
; |
; The space
; is important.
instead of (-x).
That's because - is a valid character in an identifier, so -x is a valid function name. With the space between - and x, though, it calls the function - which takes one or more arguments.
Shorter:
(defun abs-x (x)
(cond ((> x 0) x)
(t (- x))))
Reduced number of checkings using the fact that (- 0) also evaluates to0.
Instead of <= for the last check, use the simpler t - the else in common lisp cond clauses.
With if this would be:
(defun abs-x (x)
(if (> x 0)
x
(- x)))
During the execution of my code I get the following errors in the different Scheme implementations:
Racket:
application: not a procedure;
expected a procedure that can be applied to arguments
given: '(1 2 3)
arguments...:
Ikarus:
Unhandled exception
Condition components:
1. &assertion
2. &who: apply
3. &message: "not a procedure"
4. &irritants: ((1 2 3))
Chicken:
Error: call of non-procedure: (1 2 3)
Gambit:
*** ERROR IN (console)#2.1 -- Operator is not a PROCEDURE
((1 2 3) 4)
MIT Scheme:
;The object (1 2 3) is not applicable.
;To continue, call RESTART with an option number:
; (RESTART 2) => Specify a procedure to use in its place.
; (RESTART 1) => Return to read-eval-print level 1.
Chez Scheme:
Exception: attempt to apply non-procedure (1 2 3)
Type (debug) to enter the debugger.
Guile:
ERROR: In procedure (1 2 3):
ERROR: Wrong type to apply: (1 2 3)
Chibi:
ERROR in final-resumer: non procedure application: (1 2 3)
Why is it happening
Scheme procedure/function calls look like this:
(operator operand ...)
Both operator and operands can be variables like test, and + that evaluates to different values. For a procedure call to work it has to be a procedure. From the error message it seems likely that test is not a procedure but the list (1 2 3).
All parts of a form can also be expressions so something like ((proc1 4) 5) is valid syntax and it is expected that the call (proc1 4) returns a procedure that is then called with 5 as it's sole argument.
Common mistakes that produces these errors.
Trying to group expressions or create a block
(if (< a b)
((proc1)
(proc2))
#f)
When the predicate/test is true Scheme assumes will try to evaluate both (proc1) and (proc2) then it will call the result of (proc1) because of the parentheses. To create a block in Scheme you use begin:
(if (< a b)
(begin
(proc1)
(proc2))
#f)
In this (proc1) is called just for effect and the result of teh form will be the result of the last expression (proc2).
Shadowing procedures
(define (test list)
(list (cdr list) (car list)))
Here the parameter is called list which makes the procedure list unavailable for the duration of the call. One variable can only be either a procedure or a different value in Scheme and the closest binding is the one that you get in both operator and operand position. This would be a typical mistake made by common-lispers since in CL they can use list as an argument without messing with the function list.
wrapping variables in cond
(define test #t) ; this might be result of a procedure
(cond
((< 5 4) result1)
((test) result2)
(else result3))
While besides the predicate expression (< 5 4) (test) looks correct since it is a value that is checked for thurthness it has more in common with the else term and whould be written like this:
(cond
((< 5 4) result1)
(test result2)
(else result3))
A procedure that should return a procedure doesn't always
Since Scheme doesn't enforce return type your procedure can return a procedure in one situation and a non procedure value in another.
(define (test v)
(if (> v 4)
(lambda (g) (* v g))
'(1 2 3)))
((test 5) 10) ; ==> 50
((test 4) 10) ; ERROR! application: not a procedure
Undefined values like #<void>, #!void, #<undef>, and #<unspecified>
These are usually values returned by mutating forms like set!, set-car!, set-cdr!, define.
(define (test x)
((set! f x) 5))
(test (lambda (x) (* x x)))
The result of this code is undetermined since set! can return any value and I know some scheme implementations like MIT Scheme actually return the bound value or the original value and the result would be 25 or 10, but in many implementations you get a constant value like #<void> and since it is not a procedure you get the same error. Relying on one implementations method of using under specification makes gives you non portable code.
Passing arguments in wrong order
Imagine you have a fucntion like this:
(define (double v f)
(f (f v)))
(double 10 (lambda (v) (* v v))) ; ==> 10000
If you by error swapped the arguments:
(double (lambda (v) (* v v)) 10) ; ERROR: 10 is not a procedure
In higher order functions such as fold and map not passing the arguments in the correct order will produce a similar error.
Trying to apply as in Algol derived languages
In algol languages, like JavaScript and C++, when trying to apply fun with argument arg it looks like:
fun(arg)
This gets interpreted as two separate expressions in Scheme:
fun ; ==> valuates to a procedure object
(arg) ; ==> call arg with no arguments
The correct way to apply fun with arg as argument is:
(fun arg)
Superfluous parentheses
This is the general "catch all" other errors. Code like ((+ 4 5)) will not work in Scheme since each set of parentheses in this expression is a procedure call. You simply cannot add as many as you like and thus you need to keep it (+ 4 5).
Why allow these errors to happen?
Expressions in operator position and allow to call variables as library functions gives expressive powers to the language. These are features you will love having when you have become used to it.
Here is an example of abs:
(define (abs x)
((if (< x 0) - values) x))
This switched between doing (- x) and (values x) (identity that returns its argument) and as you can see it calls the result of an expression. Here is an example of copy-list using cps:
(define (copy-list lst)
(define (helper lst k)
(if (null? lst)
(k '())
(helper (cdr lst)
(lambda (res) (k (cons (car lst) res))))))
(helper lst values))
Notice that k is a variable that we pass a function and that it is called as a function. If we passed anything else than a fucntion there you would get the same error.
Is this unique to Scheme?
Not at all. All languages with one namespace that can pass functions as arguments will have similar challenges. Below is some JavaScript code with similar issues:
function double (f, v) {
return f(f(v));
}
double(v => v * v, 10); // ==> 10000
double(10, v => v * v);
; TypeError: f is not a function
; at double (repl:2:10)
// similar to having extra parentheses
function test (v) {
return v;
}
test(5)(6); // == TypeError: test(...) is not a function
// But it works if it's designed to return a function:
function test2 (v) {
return v2 => v2 + v;
}
test2(5)(6); // ==> 11
I'm a beginner with functional programming and I try to pretty print a maze.
Here is my function
(defn pprint-maze
[arr row col]
(loop [coll arr idx 0]
(match [idx]
[(_ :guard #(= (mod idx col) 0))] (println "") ; write a \n
:else (print "-")) ; write a wall
(when (next coll)
(recur (next coll) (inc idx)))))
My function takes the collection and the size of the maze and for now, just print a dash and a \n at the end of the row.
The problem I've it's :
Exception in thread "main" clojure.lang.ArityException: Wrong number of args (1) passed to: core/pprint-maze/fn--4873/fn--4874
I think the function pointed out is my loop function, and the problem is related to match (because when I comment the match block, everything work). I think that match try to call the loop function with nil as argument (the return of the println function).
How to solve that ?
The function passed to :guard should take exactly one argument, the value being guarded. Your function takes zero arguments.
i'm trying to write a code in RACKET , i know how to solve it but i'm having some trouble , i can use your help .
the function will get a list and a specific symbol , and it need to return the number of times that the symbol is shown in the list .
in the test - i'm comparing the result with a number i'm asking and should return true if the number is the same .
i've tried to do it with (if / cond / and even tried acc ) - but there is always something that is missing .
here is my code including the test .
please help me find out how to write it .
the idea of the solution is , the i take the head of the list and i check if it's equal to the symbol i wrote , if it does - n is plus 1 , empty list is equal 0 .
( : counts : (Listof Symbol) -> Integer )
(define (counts a n ) ; a = list of symbols.
(cond [(null? a) 0])
[(eq?(first a) 'x) (= n(+ n 1))]
(counts( (rest a) n)))
;test:
(test (eq? (counts ('a 'b 'x) )1))
There are several problems with your code:
The cond expression is being incorrectly used, and the else case is missing
There are erroneous parentheses, for example at the end of the second line in counts and when you call counts in the fourth line
In the base case of the recursion you must return n, the counter
You must also call the recursion if the symbol was found, in the second case
This part: (= n (+ n 1)) is not doing what you think, it's not changing the value of n, instead is testing for equality between n and (+ n 1) (which will return false, of course)
You're not passing as parameter the symbol being searched, it's hard-coded in the procedure
This is a corrected version of what you intended to write:
(define (counts a x n)
(cond [(null? a) n]
[(eq? (first a) x)
(counts (rest a) x (+ n 1))]
[else
(counts (rest a) x n)]))
Use it like this:
(counts '(a b x c d x e x) 'x 0)
=> 3
I advice you to grab a good book or tutorial, it seems you're struggling with the basic syntax. And learn how to use DrRacket to help with the syntax errors, it's an excellent IDE.