I'm experiencing a very curious problem. I have a list named "theorems" that has exactly one item. Here's proof:
[]> theorems
(((ROSES ARE RED) ^ (~ (ROSES ARE RED))))
[]> (car theorems)
((ROSES ARE RED) ^ (~ (ROSES ARE RED)))
Clearly ((ROSES ARE RED) ^ (~ (ROSES ARE RED))) is a member of the list "theorems." But when I test it for membership, it fails:
[]> (member '((ROSES ARE RED) ^ (~ (ROSES ARE RED))) theorems)
NIL
But if I call it explicitly, it works:
[]> (member (car theorems) theorems)
(((ROSES ARE RED) ^ (~ (ROSES ARE RED))))
Why is this happening, and how can I fix it?
Common Lisp uses EQL as the default test function. EQL checks whether the items are the same identical items. You want to test whether the items have the same structure. So you need to use EQUAL or EQUALP.
CL-USER 11 > (setf theorems '(((ROSES ARE RED) ^ (~ (ROSES ARE RED)))))
(((ROSES ARE RED) ^ (~ (ROSES ARE RED))))
CL-USER 12 > (member '((ROSES ARE RED) ^ (~ (ROSES ARE RED))) theorems)
NIL
Tell MEMBER to use EQUAL:
CL-USER 13 > (member '((ROSES ARE RED) ^ (~ (ROSES ARE RED)))
theorems
:test 'equal)
(((ROSES ARE RED) ^ (~ (ROSES ARE RED))))
Related
I need to be able to compare two cars of a list to sort them im LISP.
Lists '(e d) (a b)
I want to compare the cars (e and a). This works using eql. If they don't match, I want to order the lists alphabetically, so (a b) (e d).
I'm missing the part where I can see which character is 'bigger', so the check if e or a should come first. I've tried converting them to ascii codes, but that doesn't work for (car a). Using arithmetic operators such as '<' and '>' also doesn't work. Does anyone have an idea on how to do this?
Use string> without symbol-name:
CL-USER 6 > (string> 'a 'b)
NIL
CL-USER 7 > (string< 'a 'b)
0
For the sake of completeness, here is how you should use it inside sort to achieve desired result (sort is destructive- modifies used sequence, so I also used copy-tree to avoid that effect):
(let ((data '((e d) (a b))))
(sort (copy-tree data)
(lambda (x y) (string< (car x) (car y)))))
((A B) (E D))
A symbol is distinct from a string.
CL-USER> (symbol-name 'foo)
"FOO"
A string (a sequence of characters) can be compared in the manner you seem to be interested in.
CL-USER> (string> "FOO" "BOO")
0
CL-USER> (string< "FOO" "BOO")
NIL
I am trying to define a procedure that takes an integer and returns its representation in Church numerals. Could any one please help me figure out the mistake I am making? The following code it's what I have been able to do so far.
(define succ
(lambda (cn)
(lambda (f)
(lambda (x)
(f ((cn f) x))))))
(define (n->cn n)
(if (= n 0)
zero
(succ (n->cn (lambda (x) (- x 1))))))
When I run the test:
(test (num->cn 3) three)
I am getting the following error:
exception (num->cn 3) at line 107
expected: <no-expected-value>
=: contract violation
expected: number?
given: #<procedure:...ad/racket-file.rkt:99:21>
argument position: 1st
other arguments...:
0
It seems it's expecting a number? but a procedure is given. Which I think matches the intention of the procedure? Thanks for your help and comments for a newbie.
The argument to n->ch should be a number, not a procedure:
(define (n->cn n)
(if (= n 0)
zero
(succ (n->cn (- n 1)))))
I am very new to ACL2 so I understand that some of you may feel this is such a simple solution that you would frown upon my outreach for help. I am trying to figure out how to get my code to add up to an Nth reciprocal squared (I.E if n=4 then i am looking for 1/1 + 1/4 + 1/9 + 1/16)
I have a function that will add up to n and it works and looks like this
(defun sum-up-to-n (n)
(if (zp n)
0
(+ n (sum-up-to-n (- n 1)))))
With the the Reciprocal squared looking like this
(defun sum-up-to-nSqRecip (n)
(if (zp n)
0
(+ (sum-up-to-nSqRecip (- n 1))) 1/n^2) ))
I receive this error "The body of
SUM-UP-TO-NSQRECIP contains a free occurrence of the variable symbol
|1/N^2|. Note that |1/N^2| occurs in the context of condition (NOT (ZP N))
from a surrounding IF test." and i do not know how to resolve this error.
included stuff
(encapsulate nil
(set-state-ok t)
(program)
(defun check-expect-fn (left right sexpr state)
(if (equal left right)
(mv nil (cw "check-expect succeeded: ~x0~%" sexpr) state)
(er soft nil "check-expect failed: ~x0
Expected: ~x1
Actual: ~x2" sexpr right left)))
(defmacro check-expect (&whole sexpr left right)
`(check-expect-fn ,left ,right (quote ,sexpr) state))
(logic))
(include-book "doublecheck" :uncertified-okp t :dir :teachpacks)
(include-book "arithmetic-5/top" :uncertified-okp t :dir :system)
ACL2 uses LISP syntax, which means you need prefix operators. So 1/n^2 should be (/ 1 (* n n)).
LISP allows a lot of the characters to be in a name, 1/n^2 in your example is treated as a name of a variable, which isn't binded to anything (not an input either). This is why you are receiving the "free occurrence of the variable" error.
I don't get why
(setq a_sym 'abc)
(print (eq a_sym 'abc))
(print (eq 'x 'x))
(print (eq (first '('x 2 3)) 'x))
prints
T
T
NIL
Why the symbol 'x in the third statement is handled differently than second ? And, down to earth, how to compare them for identity ?
If you trace what you are comparing, you will see your mistake right away:
[1]> (eq (first '('x 2 3)) 'x)
NIL
[2]> (trace eq)
** - Continuable Error
TRACE(EQ): #<PACKAGE COMMON-LISP> is locked
If you continue (by typing 'continue'): Ignore the lock and proceed
The following restarts are also available:
ABORT :R1 Abort main loop
Break 1 [3]> :c
WARNING: TRACE: redefining function EQ in top-level, was defined in C
;; Tracing function EQ.
(EQ)
[4]> (eq (first '('x 2 3)) 'x)
1. Trace: (EQ ''X 'X) ; <======= note 1
1. Trace: EQ ==> NIL
NIL
[5]> (eq (first '(x 2 3)) 'x)
1. Trace: (EQ 'X 'X) ; <======= note 2
1. Trace: EQ ==> T
T
IOW, you are "overquoting" your x: when you type 'x, it's the same as (quote x) so, you are checking for equality of symbol x and list (quote x) and, of course, getting nil.
Notes:
(eq ''x 'x): since eq is a function, its arguments are evaluated and we are comparing 'x == (quote x) with x and getting nil.
(eq 'x 'x): for the same reason, we are comparing x with x and getting t.
Related:
Lisp quote work internally
Confused by Lisp Quoting
When to use 'quote in Lisp
Lisp: quoting a list of symbols' values
Syntax and reading
You wrote:
symbol 'x
Note that 'x is not a symbol. It's a quote character in front of a symbol. The quote character has a special meaning in s-expressions: read the next item and enclose it in (quote ...).
Thus 'x is really the list (quote x).
CL-USER 9 > (read-from-string "'x")
(QUOTE X)
2
Evaluation
A quoted object is not evaluated. The quote is a special operator, which means it is built-in syntax/semantics in Common Lisp and not a function and not a macro. The evaluator returns the quoted object as such:
CL-USER 10 > (quote x)
X
Your example
(eq (first (quote ((quote x) 2 3))) (quote x))
Let's evaluate the first part:
CL-USER 13 > (first (quote ((quote x) 2 3)))
(QUOTE X)
The result is the list (quote x).
Let's evaluate the second part:
CL-USER 14 > 'x
X
So the result is the symbol x.
x and (quote x) are not eq.
Evaluation and Quoting
'('x 2 3)
What would be the purpose of the second quote in the list?
The first quote already means that the WHOLE following data structure is not to be evaluated. Thus it is not necessary to quote a symbol inside to prevent its evaluation. If a list is quoted, none of its sublists or subelements are evaluated.
Summary
The quote is not part of the symbol. It is a built-in special operator used to prevent evaluation.
What is the exclusive or functions in scheme? I've tried xor and ^, but both give me an unbound local variable error.
Googling found nothing.
I suggest you use (not (equal? foo bar)) if not equals works. Please note that there may be faster comparators for your situiation such as eq?
As far as I can tell from the R6RS (the latest definition of scheme), there is no pre-defined exclusive-or operation. However, xor is equivalent to not equals for boolean values so it's really quite easy to define on your own if there isn't a builtin function for it.
Assuming the arguments are restricted to the scheme booleans values #f and #t,
(define (xor a b)
(not (boolean=? a b)))
will do the job.
If you mean bitwise xor of two integers, then each Scheme has it's own name (if any) since it's not in any standard. For example, PLT has these bitwise functions, including bitwise-xor.
(Uh, if you talk about booleans, then yes, not & or are it...)
Kind of a different style of answer:
(define xor
(lambda (a b)
(cond
(a (not b))
(else b))))
Reading SRFI-1 shed a new light upon my answer. Forget efficiency and simplicity concerns or even testing! This beauty does it all:
(define (xor . args)
(odd? (count (lambda (x) (eqv? x #t)) args)))
Or if you prefer:
(define (true? x) (eqv? x #t))
(define (xor . args) (odd? (count true? args)))
(define (xor a b)
(and
(not (and a b))
(or a b)))
Since xor could be used with any number of arguments, the only requirement is that the number of true occurences be odd. It could be defined roughly this way:
(define (true? x) (eqv? x #t))
(define (xor . args)
(odd? (length (filter true? args))))
No argument checking needs to be done since any number of arguments (including none) will return the right answer.
However, this simple implementation has efficiency problems: both length and filter traverse the list twice; so I thought I could remove both and also the other useless predicate procedure "true?".
The value odd? receives is the value of the accumulator (aka acc) when args has no remaining true-evaluating members. If true-evaluating members exist, repeat with acc+1 and the rest of the args starting at the next true value or evaluate to false, which will cause acc to be returned with the last count.
(define (xor . args)
(odd? (let count ([args (memv #t args)]
[acc 0])
(if args
(count (memv #t (cdr args))
(+ acc 1))
acc))))
> (define (xor a b)(not (equal? (and a #t)(and b #t))))
> (xor 'hello 'world)
$9 = #f
> (xor #f #f)
$10 = #f
> (xor (> 1 100)(< 1 100))
$11 = #t
I revised my code recently because I needed 'xor in scheme and found out it wasn't good enough...
First, my earlier definition of 'true? made the assumption that arguments had been tested under a boolean operation. So I change:
(define (true? x) (eqv? #t))
... for:
(define (true? x) (not (eqv? x #f)))
... which is more like the "true" definition of 'true? However, since 'xor returns #t if its arguments have an 'odd? number of "true" arguments, testing for an even number of false cases is equivalent. So here's my revised 'xor:
(define (xor . args)
(even? (count (lambda (x) (eqv? x #f)) args)))