In Paul Graham's book ANSI Common Lisp, while discussing symbol vs string manipulation, he says
Symbols can be compared in one step with eql ..."
(p138). Here are two cases of symbol comparison:
(setq x 3)
3
(setq a 'x)
X
(setq b 'x)
X
(eq a b)
T
So, since a and b point to the same symbol object, named "X", they are eq. However, this contrasts with
(setq a (copy-symbol 'x))
#:X
(setq b (copy-symbol 'x))
#:X
(symbol-name a)
"X"
(symbol-name b)
"X"
(eq a b)
NIL
(eql a b)
NIL
(equal a b)
NIL
(equalp a b)
NIL
Now, a and b point to different symbol objects, even though they have the same symbol-name and same print name. My questions are:
why does Graham say eql rather than eq, and
in the second case, why are a and b not at least equalp?
eql over eq?
eql is "more predictable" in the sense that it is less implementation-dependent.
E.g., non-immediate numbers (i.e.,
non-fixnums) can be non-eq
in some implementations and eq in others:
(= 1.5d0 1.5d0)
=> T
(eql 1.5d0 1.5d0)
=> T
(eq 1.5d0 1.5d0)
=> T in some implementations, NIL in others
why aren't the identically named symbols not equalp?
That's what the spec says :-)
Note that it does violated the rule of thumb:
A rough rule of thumb is that two objects are equal if and only if their printed representations are the same.
The main non-histerical reason for this is that symbol, while being
"atomic", still carries many baggage (e.g., variable and function
bindings). IOW, a symbol is
much more than its name.
Also, one generally expects that equal forms evaluate to
equal values, which cannot be the case if different symbols (with
possibly different bindings!) are evaluated as equal.
Now, the only differences between equal and equalp are
case-insensitive character (and string) comparison
comparing numbers using =
recursively descending into
structure,
array, and
hash-table.
None of these changes affects the "similar evaluation rule" above,
so there is no reason for equalp to compare symbols based on
their names.
If you want to compare symbols by names, you can
use string=:
(eq '#:a '#:a)
==> NIL
(equalp '#:a '#:a)
==> NIL
(string= '#:a '#:a)
==> T
Related
I have a program that requires having a series of interchangeable functions.
In c++ I can do a simple typedef statement. Then I can call upon on a function in that list with function[variable]. How can I do this in Common Lisp?
In Common Lisp everything is a object value, functions included. (lambda (x) (* x x)) returns a function value. The value is the address where the function resides or something similar so just having it in a list, vector og hash you can fetch that value and call it. Here is an example using lists:
;; this creates a normal function in the function namespace in the current package
(defun sub (a b)
(- a b))
;; this creates a function object bound to a variable
(defparameter *hyp* (lambda (a b) (sqrt (+ (* a a) (* b b)))))
;; this creates a lookup list of functions to call
(defparameter *funs*
(list (function +) ; a standard function object can be fetched by name with function
#'sub ; same as function, just shorter syntax
*hyp*)) ; variable *hyp* evaluates to a function
;; call one of the functions (*hyp*)
(funcall (third *funs*)
3
4)
; ==> 5
;; map over all the functions in the list with `3` and `4` as arguments
(mapcar (lambda (fun)
(funcall fun 3 4))
*funs*)
; ==> (7 -1 5)
A vector of functions, where we take one and call it:
CL-USER 1 > (funcall (aref (vector (lambda (x) (+ x 42))
(lambda (x) (* x 42))
(lambda (x) (expt x 42)))
1)
24)
1008
The already given answers having provided plenty of code, I'd like to complement with a bit of theory. An important distinction among languages is whether or not they treat functions as first-class citizens. When they do, they are said to support first-class functions. Common Lisp does, C and C++ don't. Therefore, Common Lisp offers considerably greater latitude than C/C++ in the use of functions. In particular (see other answers for code), one creates arrays of functions in Common Lisp (through lambda-expressions) much in the same way as arrays of any other object. As for 'pointers' in Common Lisp, you may want to have a look here and here for a flavour of how things are done the Common Lisp way.
I am trying to implement tail call recursive factorial in Common Lisp, to try it and just experience it.
I copied some code and rewrote it in Common Lisp like so:
(defun tailrecsum (x &key (running-total 0 running-total-p))
(if (= x 0)
(if running-total-p running-total 0)
(tailrecsum (- x 1) (+ running-total x))))
However, I get first a warning:
SIMPLE-WARNING:
The function has an odd number of arguments in the keyword portion.
And when trying to run it, I get an error:
SIMPLE-PROGRAM-ERROR:
odd number of &KEY arguments
Why can't I have an odd number of keyword arguments? What's the problem with that and what can I do about it?
For example in Python I could write:
def func(a, b=10):
print([a, b])
So I would have an odd number, one, of keyword arguments. No issues there.
The error doesn't refer to the number of keyword parameters; rather it means the number of arguments you call the function with. Since keywords arguments by definition need to be in pairs (:KEYWORD VALUE), having odd number of arguments means you must be missing something.
In this case you're missing the keyword in
(tailrecsum (- x 1) (+ running-total x))
which should be
(tailrecsum (- x 1) :running-total (+ running-total x))
I'm doing some exercises in Racket, and ran into a problem I couldn't seem to query the docs for.
I want to generate the following curries of modulo for a list of divisors:
(define multlist '[3 5])
(define modfuncs (map (lambda x ;# make some modulos
(curry modulo x)) multlist))
This produces a list of curried procedures, which sounds promising, but when I try to test one of them, I get the following error:
-> (car modfuncs)
#<procedure:curried>
-> ((car modfuncs) 3)
; modulo: contract violation
; expected: integer?
; given: '(3)
; argument position: 1st
; [,bt for context]
Assuming this isn't a terrible way to do this, how do I unquote the values of multlist passed to the curry/map call so these functions will evaluate correctly?
You're actually doing this correctly, albeit with a tiny mistake:
(lambda x (curry modulo x))
This doesn't do what you think it does. What you actually want is this:
(lambda (x) (curry modulo x))
See the difference? In the former, x is not within an arguments list, so it will actually be passed a list of all arguments passed to the function, not a single argument.
You can see this behavior for yourself with the following simple program:
((lambda x x) 1 2 3)
; => '(1 2 3)
Therefore, your curry function is receiving a list of one number for x, not an actual integer.
So perhaps the more satisfying answer is: why does Racket do this? Well, this is actually a result of Racket/Scheme's rest parameter syntax. Inserting a dot before the last argument of a lambda makes that parameter a rest parameter, which becomes a list that holds all additional parameters passed to the function.
((lambda (a b . rest) rest) 1 2 3 4 5)
; => '(3 4 5)
However, this isn't actually just a special syntax. The dot notation actually has to do with how Racket's reader reads lists and pairs in syntax. The above parameter list actually becomes an "improper" list made up of the following cons sequence:
(cons 'a (cons 'b 'rest))
The same function without the rest parameter would have a proper list as its argument declaration, which would look like this instead:
(cons 'a (cons 'b null))
So then, what about the original x just standing alone? Well, that's an improper list with no preceding arguments! Doing ( . rest) wouldn't make any sense—it would be a syntax error—because you'd be trying to create a pair with no car element. The equivalent is just dropping the pair syntax entirely.
So I am very new to lisp, and not that advanced of a programmer yet. Just getting started really.
I'm messing around trying to get a very simple genetic algorithm going from scratch, and while most of my code seems to execute as desired, I'm stuck with such a simple bug/misunderstanding that I am blocked from the bottom... There is clearly something I am not getting despite my hours online trying to find a solution..
Basically, I know it has something to do with the fact I am trying to call a variable as if it were an operator (which the variable holds) and thus it tells me my function (which is really just a variable holding an operator) is not defined.
The start of my code works fine.
(defvar *nb* 8)
(defvar *random-operators* '(+ - * /))
(defun get-operator ()
"Returns an element of the list of operators chosen at random."
(nth (random (length *random-operators*)) *random-operators*))
So (get-operator) does get me one of the four random operators, as desired.
I used this even simpler code to test the structure of my code, and it works as desired.
(defun ga-simple ()
"Returns the value of genome once it matches *nb* and prints
the number of generations required."
(do ((genome (random 10) (random 10))
(generation-counter 1 (incf generation-counter)))
((eql genome *nb*)
(format t
"The solution is ~S, and it took ~D generations"
genome
generation-counter))))
The problem comes when I try and create a genome composed of three variables, one holding the operator, and the other two, the integers.
(defun ga-with-operator ()
(do ((genome 42 (opr1 int1 int2))
(generation-counter 0 (incf generation-counter))
(opr1 + (get-operator)
(int1 (random 10) (random 10))
(int2 (random 10) (random 10))
((eql genome *nb*)
(format t
"The solution is ~S, and it took ~D generations"
genome
generation-counter))))
my compiler warnings tell me where the problem is...
;Compiler warnings for "./ga-one.lisp" :
; In GA-WITH-OPERATOR: Unused lexical variable OPR1
;Compiler warnings for "./ga-one.lisp" :
; In GA-WITH-OPERATOR: Undefined function OPR1/
And so clearly a call to the function (ga-with-operator) says opr1 is an undefined-function-call. So from what I gather when the "do" macro checks the increment condition for the variable genome, it reads the list, expecting opr1 to be an operator and not a variable holding an operator... Now, an operator simply entered works perfect here, but I don't know how to make lisp use the evaluated value of opr1, which is an operator, as the operator for the integers...
To simplify, I made a function trying to construct a single genome using my get-operator function, and failed hard even there lol
(defun get-genome ()
(let ((operator1 (get-operator)))
(operator1 (random 10) (random 10))))
So I made a test-let function to make sure my "let" variable assignment is right...
(defun test-let ()
(let ((rand (get-operator)))
(print rand)))
Which it does... So now I am getting desperate and missing something obviously very simple and yet crucial to make it all stick together.
If someone could explain to me, or just show me, how to get the simple (get-genome) function to work I would really appreciate it. I know lisp expects an operator as the first element in the list, and my error stems from me feeding it a variable holding an operator instead... How do I convince it my variable is the operator it is holding?
in case anyone wants to know the working code...
(defvar *nb* 42)
(defvar *random-operators* '(+ - * /))
(defun get-operator ()
"Return an element of the list of operators chosen at random."
(nth (random (length *random-operators*)) *random-operators*))
(defun get-genome ()
(let ((operator1 (get-operator)))
(funcall operator1 (+ 1 (random 9)) (+ 1 (random 9)))))
(defun ga-with-operator ()
(do ((genome (get-genome) (get-genome))
(generation-counter 0 (1+ generation-counter)))
((eql genome *nb*)
(format t "The solution is ~S, and it took ~D generations"
genome generation-counter))))
In Common Lisp you need to use FUNCALL if the function is returned by a function or if the function is stored in a variable.
(defun get-genome ()
(let ((operator1 (get-operator)))
(funcall operator1 (random 10) (random 10))))
Common Lisp has different namespaces for functions and for values. Thus a name can both be a variable and a function:
(defun foo (list)
(list list list))
The first list in the last line calls the global function called list and the second and third are actually the local variable list.
(defun ga-with-operator ()
(do ((genome 42 (opr1 int1 int2))
(generation-counter 0 (incf generation-counter))
(opr1 + (get-operator)
(int1 (random 10) (random 10))
(int2 (random 10) (random 10))
((eql genome *nb*)
(format t
"The solution is ~S, and it took ~D generations"
genome
generation-counter))))
Above function has more problems.
Can you see the first? Hint: try indenting it correctly. Are the parentheses correct?
also: + is not a function. It is a variable in your code. To make it a function you would need to quote or function quote it. '+ or #'+.
you also don't need to INCF the generation-counter. Just adding 1 is fine. The DO loop updates the variable. Use (1+ generation-counter).
Of course, FUNCALL / APPLY is what you want, but just to complete the picture, note that this also works:
(setf (symbol-function 'operator1) (get-operator))
(operator1 (+ 1 (random 9)) (+ 1 (random 9)))
The reason you generally don't want to do this is that binding of the symbol-function slot is global.
CLHS states "The &optional and &rest markers can appear in the value-type list; they indicate the parameter list of a function that, when given to multiple-value-call along with the values, would correctly receive those values." Can someone please explain what this means?
CLHS states elsewhere that the VALUES type specifier "can be used only as the value-type in a function type specifier or a the special form". This being the case, I don't understand how CLHS can talk about how a VALUES type specifier can "indicate the parameter list of a function". It seems to be a contradiction, not to mention an inscrutable saying. How can the type declaration of a function's value-type (return type) say something about the function's (or some other function's--it's not really clear) formal parameters?
EDIT:
I asked this question on the sbcl-help list, and one of the devs responded "&optional appears, therefore the VALUES list is treated as the full argument-like syntax. It corresponds to a function with type (FUNCTION (SYMBOL &OPTIONAL) *), i.e. a function of exactly one argument that must be of type SYMBOL."
Here's how I understand this passage. At least it makes sense to me, so I may claim understanding it :)
I think that what it is trying to say is that the (values ...) form must be compatible with the arguments lambda list of the function, which, if called from multiple-value-call would have received these arguments correctly.
In other words, suppose there are two functions: f and g. f returns values: (integer &rest more-integers) - in human language: it can return one ore more integers. Then, if you want to call (multiple-value-call #'g (f)) you must (you is a compiler, actually) make sure that the signature of g is (ftype (function (integer &rest more-integers) t)). The return type of g doesn't matter.
Here's my attempt at making an example illustrating it:
CL-USER> (declaim (ftype (function () (values integer &rest more-integers)) f))
; No value
CL-USER> (defun f () (values 1 2))
F
CL-USER> (declaim (ftype (function (integer &rest more-integers) t) g))
; No value
CL-USER> (defun g (x &rest rest) (reduce #'+ rest :initial-value x))
G
CL-USER> (multiple-value-call #'g (f))
3
CL-USER> (declaim (ftype (function (single-float) t) x))
; No value
CL-USER> (defun x (y) y)
X
CL-USER> (multiple-value-call #'g (x 1.5))
; Evaluation aborted on #<TYPE-ERROR expected-type: INTEGER datum: 1.5>.