How can I modify function bindings in Common Lisp? - variables

Here is something you can do in Scheme:
> (define (sum lst acc)
(if (null? lst)
acc
(sum (cdr lst) (+ acc (car lst)))))
> (define sum-original sum)
> (define (sum-debug lst acc)
(print lst)
(print acc)
(sum-original lst acc))
> (sum '(1 2 3) 0)
6
> (set! sum sum-debug)
> (sum '(1 2 3) 0)
(1 2 3)
0
(2 3)
1
(3)
3
()
6
6
> (set! sum sum-original)
> (sum '(1 2 3) 0)
6
If I were to do the following in Common Lisp:
> (defun sum (lst acc)
(if lst
(sum (cdr lst) (+ acc (car lst)))
acc))
SUM
> (defvar sum-original #'sum)
SUM-ORIGINAL
> (defun sum-debug (lst acc)
(print lst)
(print acc)
(funcall sum-original lst acc))
SUM-DEBUG
> (sum '(1 2 3) 0)
6
Now how can I do something like (setf sum #'sum-debug) that would change the binding of a function defined with defun?

Because Common Lisp has a different namespace for functions, you need to use symbol-function or fdefinition.
CL-USER> (defun foo (a)
(+ 2 a))
FOO
CL-USER> (defun debug-foo (a)
(format t " DEBUGGING FOO: ~a" a)
(+ 2 a))
DEBUG-FOO
CL-USER> (defun debug-foo-again (a)
(format t " DEBUGGING ANOTHER FOO: ~a" a)
(+ 2 a))
DEBUG-FOO-AGAIN
CL-USER> (foo 4)
6
CL-USER> (setf (symbol-function 'foo) #'debug-foo)
#<FUNCTION DEBUG-FOO>
CL-USER> (foo 4)
DEBUGGING FOO: 4
6
CL-USER> (setf (fdefinition 'foo) #'debug-foo-again)
#<FUNCTION DEBUG-FOO-AGAIN>
CL-USER> (foo 4)
DEBUGGING ANOTHER FOO: 4
6
CL-USER>

A typical use case for redefining functions in such a way is during debugging. Your example indicates that. Common Lisp already provides higher-level machinery for that: TRACE. Under the hood it sets the function value of the symbol, but it provides a more convenient user interface. Often something like TRACE will have many, non-standard, options.
Tracing functions
The following example uses Clozure Common Lisp, which is always compiling code:
? (defun sum (list accumulator)
(declare (optimize debug)) ; note the debug declaration
(if (endp list)
accumulator
(sum (rest list) (+ accumulator (first list)))))
SUM
? (sum '(1 2 3 4) 0)
10
? (trace sum)
NIL
? (sum '(1 2 3 4) 0)
0> Calling (SUM (1 2 3 4) 0)
1> Calling (SUM (2 3 4) 1)
2> Calling (SUM (3 4) 3)
3> Calling (SUM (4) 6)
4> Calling (SUM NIL 10)
<4 SUM returned 10
<3 SUM returned 10
<2 SUM returned 10
<1 SUM returned 10
<0 SUM returned 10
10
Then to untrace:
? (untrace sum)
Advising functions
In your example you have printed the arguments on entering the function. In many Common Lisp implementations there is another mechanism to augment functions with added functionality: advising. Again using Clozure Common Lisp and its advise macro:
? (advise sum ; the name of the function
(format t "~%Arglist: ~a" arglist) ; the code
:when :before) ; where to add the code
#<Compiled-function (CCL::ADVISED 'SUM) (Non-Global) #x302000D7AC6F>
? (sum '(1 2 3 4) 0)
Arglist: ((1 2 3 4) 0)
Arglist: ((2 3 4) 1)
Arglist: ((3 4) 3)
Arglist: ((4) 6)
Arglist: (NIL 10)
10

Related

Basic Lisp function - sum of even minus sum of odd

I'm trying to write a function which takes as parameter a List and calculates the sum of even numbers minus the sum of odd numbers.
Here is my implementation but I have no clue why it is not working as expected, could you give me any hints about whats wrong?
(defun sumEvenOdd (R)
(cond
((NULL R) 0)
((and (ATOM (CAR R)) (= (mod (CAR R) 2) 0))
(+ (CAR R) (sumEvenOdd (CDR R))))
((and (ATOM (CAR R)) (not (= (mod (CAR R) 2) 0)))
(- (CAR R) (sumEvenOdd (CDR R)) ))
((LISTP (CAR R)) (sum (sumEvenOdd (CAR R)) (sumEvenOdd (CDR R))))
(T (sumEvenOdd (CDR R)))
)
)
Regarding the code algorithm, it fails because how the math is being done.
How the code is now, this the evaluation being done with the list (list 1 2 3 4 5) is (- 1 (+ 2 (- 3 (+ 4 (- 5 0))))) that equals 5.
What we were expecting was (2+4)-(1+3+5) that equals -3. What's wrong?
Basically the sum operation in math is commutative, while the subtraction operation is not. 1+5 and 5+1 is the same. 1-5 and 5-1 is not.
This reflects on the code on the last operation where 5 is being subtracted 0.
The simplest solution is to adjust the operation order, switching the arguments.
(defun sumEvenOdd (R)
(cond
((NULL R) 0)
((and (ATOM (CAR R)) (= (mod (CAR R) 2) 0))
(+ (sumEvenOdd (CDR R)) (CAR R)))
((and (ATOM (CAR R)) (not (= (mod (CAR R) 2) 0)))
(- (sumEvenOdd (CDR R)) (CAR R)))
)
)
That way the evaluation will be: (- (+ (- (+ (- 0 1) 2) 3) 4) 5) that equals -3.
PS: You can check and test the code here: http://goo.gl/1cEA5i
You are almost there. Here is an edited version of your code:
(defun sumEvenOdd (R)
(cond
((NULL R) 0)
((and (ATOM (CAR R))
(= (mod (CAR R) 2) 0))
(+ (sumEvenOdd (CDR R)) (CAR R))) ; switched places for consistency
((and (ATOM (CAR R)) (not (= (mod (CAR R) 2) 0)))
(- (sumEvenOdd (CDR R)) (CAR R))) ; operands needed to be switched
((LISTP (CAR R)) (+ (sumEvenOdd (CAR R)) ; what is sum? replaced with +
(sumEvenOdd (CDR R))))
(T (sumEvenOdd (CDR R)))))
Here is a solution using reduce:
(defun sum-even-odd (list)
(reduce (lambda (acc e)
(cond ((consp e) (+ acc (sum-even-odd e)))
((not (numberp e)) acc) ; perhaps not needed
((oddp e) (- acc e))
(t (+ acc e))))
list
:initial-value 0))
(sum-even-odd '(1 2 (3 4 (5 6) 7) 8 9 10)) ; ==> 5
If you are certain the list only has numbers or other lists with numbers the check for something that is not consp nor numberp would be redundant. This does not work for dotted lists.
There are answers with how to fix your code, but let's look at a different implementation.
You don't specify that your function needs to work on a tree so this is for a flat list of numbers.
(defun sum-even-odd (r)
(- (apply #'+ (remove-if-not #'evenp r))
(apply #'+ (remove-if-not #'oddp r))))
remove-if-not takes a list and a predicate function. It run the predicate on each element of the list and create a new list only containing the elements where the predicate didn't return nil.
apply takes a function and a list and calls the function with the arguments being the elements of the list. so (apply #'+ '(1 2 3 4)) is equivalent to (+ 1 2 3 4)
Common lisp has good functions for workings with lists (and many other data types) check em out and your code can end up much cleaner.
Also never use camel-case (or any case based naming) in common lisp and symbols are case insensitive. The symbols HeLloThErE and hellothere and helloThere are the same symbol. This is why you will see hyphens used in names.

What's wrong with my tail-recursive sum procedure?

What's wrong with my tail-recursive sum procedure? My tail-recursive scheme procedure will not run.
Code:
(define (sum term a next b)
(define iter result i)
(if (> i b)
result
(iter (+ result (term i)) (next i))
(iter 0 a )))
(define (increment x)(+ x 1))
(define (sum-square a b)
(sum (lambda(x)(* x x)) a increment b))
(define (sum-int a b)
(define (identity a) a)
(sum identity a increment b))
(sum-int 5 10)
(sum-square 5 10)
Error:
Error: execute: unbound symbol: "result" [sum-int, (anon), sum, (anon), sum-square, sum, (anon)]
You have parentheses problems in sum. Try this:
(define (sum term a next b)
(define (iter result i)
(if (> i b)
result
(iter (+ result (term i)) (next i))))
(iter 0 a))
In particular, notice that this line was wrong, that's not how you define a procedure:
(define iter result i)
And the corresponding closing parentheses is wrong, too. A strict discipline of correctly indenting and formatting the code will make these kind of errors easier to catch, use a good IDE for this.

Simulating Static Variables in Scheme

A function that returns how many times it has been called in Scheme would look like
(define count
(let ((P 0))
(lambda ()
(set! P (+ 1 P))
P)))
(list (count) (count) (count) (count)) ==> (list 1 2 3 4)
But suppose that we have an expression that looks like this
(map ______ lst)
and we want that to evaluate to
(list 1 2 3 ... n)
where n = (length list)
The problem requires we use a lambda statement in the blank, and we cannot use any auxiliary definitions like (count) in the blank, so
(lambda (x) (count))
is not allowed. Simply replacing (count) with the previous definition like this:
(map
(lambda (x)
((let ((P 0))
(lambda ()
(set! P (+ 1 P))
P))))
L)
doesn't work either.
Any suggestions?
You're very, very close to a correct solution! in the code in the question just do this:
The outermost lambda is erroneous, delete that line and the corresponding closing parenthesis
The innermost lambda is the one that will eventually get passed to the map procedure, so it needs to receive a parameter (even though it's not actually used)
Delete the outermost parenthesis surrounding the let form
It all boils down to this: the lambda that gets passed to map receives a parameter, but also encloses the P variable. The let form defines P only once in the context of the passed lambda, and from that point on the lambda "remembers" the value of P, because for each of the elements in the list the same P is used.
You're 90% of the way there. Use the right-hand-side of your count definition in the blank, and add an (ignored) argument to the function.
(define add-stat-var
(let ( (P '()) )
(lambda (x1)
(if (equal? x1 "ResetStatVar") (set! P '()) (set! P (cons x1 P)))
P
) ;lambda
) ;let
) ;define
(define (test-add-stat-var x)
(let* ( (result '()) )
(set! result (add-stat-var 12))
(set! result (add-stat-var 14))
(set! result (add-stat-var 16))
(display (add-stat-var x)) (newline)
(set! result (add-stat-var "ResetStatVar"))
(display (cdr (add-stat-var x))) (newline)
)
)

Scheme programming sum function overloading

Define a function sum, which takes two numbers, or two real functions, and returns their sum. E.g.
(sum 1 2) => 3
((sum cos exp) 0) => 2
I get that for the sum of two numbers the code would be the following:
(define sum (lambda (x y)
(+ x y)))
But what would be the code for the two real functions...? How would I do this? can anyone please help?
Also how would I do this:
Define a function sum-all which works like sum, but works on a list of numbers or a list of functions. Assume the list contains at least one element.
E.g.
(sum-all (list 1 2 3)) => 6
((sum-all (list cos sin exp)) 0) => 2
Note: this is not homework... I was going through a past midterm.
For the first part of your question, I'll have to agree with PJ.Hades that this is the simplest solution:
(define (sum x y)
(if (and (number? x) (number? y))
(+ x y)
(lambda (n)
(+ (x n) (y n)))))
For the second part, we can make good use of higher-order procedures for writing a simple solution that is a generalization of the previous one:
(define (sum-all lst)
(if (andmap number? lst)
(apply + lst)
(lambda (n)
(apply + (map (lambda (f) (f n)) lst)))))
In both procedures, I'm assuming that all the operands are of the same kind: they're either all-numbers or all-functions, as inferred from the sample code provided in the question.
Do you mean this?
(define (sum a b)
(if (and (number? a) (number? b))
(+ a b)
(lambda (x)
(+ (a x) (b x)))))
(define (sum lst)
(cond
[(empty? lst) 0]
[else (foldr + 0 lst)]))
((lambda (a b) (+ a b)) 4 5)
this how it is done using lambda.
using define we can write as follows
(define (sum a b) (+ a b))
I'm a little rusty with my Scheme, so perhaps there's a better way to do this, but you could do:
(define (sum-all lst)
(define (sum-funcs-helper funcs x)
(if (empty? funcs)
0
(+ ((car funcs) x)
(sum-funcs-helper (cdr funcs) x))))
(if (empty? lst)
0 ;; Beats me what this is supposed to return.
(if (number? (car lst))
(apply + lst)
(lambda (x) (sum-funcs-helper lst x)))))

Unbound Variable on Function Name

I'm writing a program in Lisp(common lisp dialect)..
I want the program to count the number of sublists in a list..
This is what I have written till now:
(defun llength (L)
(cond
((null L) 0)
((list (first L)) (progn (+ (llength (first L)) 1) (llength (rest L))))
((atom (first L)) (llength (rest L)))
)
)
The function returns the error "Unbound variable: LLENGTH" and I don't understand why or how I can fix it..
Any suggestions ?
You have multiple errors in your code.
First of all, list function creates new list, not checking if it is a list. The function you need is listp - "p" at the end means "predicate".
Second, (progn (+ (llength (first L)) 1) (llength (rest L)) will not increase counter. progn performs expressions one by one and returns result of the last expression, other results are just thrown out. progn is there mostly for side effects. What you actually need is addition of all three components: 1 to indicate one found list, result of applying function to the first element and result for applying to the rest. So, this line must be:
((listp (first L)) (+ (llength (first L)) (llength (rest L)) 1))
More errors may exist, please, be careful to indent code correctly - it really helps to reduce them.
When you define a function with the (defun function name (parameters)) call you must then call the function by typing:
(function name (parameters))
Perhaps you were simply typing:
function name (parameters)
Doing this will get you the error you are receiving so be sure to encompass your whole statement in parenthesis.
(defun llength (list)
(cond
((null list) 0)
((listp (first list))
;; 1 + the count of any sub-lists in this sub-list + the
;; count of any sub-lists in the rest of the list.
(+ 1 (llength (first list))
(llength (rest list))))
(t (llength (rest list)))))
Test:
> (llength '(1 2 3 4))
0
> (llength '(1 2 (3 4)))
1
> (llength '(1 2 (3 (4))))
2
> (llength '(1 2 (3 4) (5 6) (7 8) (9 (10 (11)))))
6