I want to make user-program which extract elements a which have element b (given by parameter) as pair in list.
Like, if I give c as parameter and list ((c a) (c b) (d f) (d g)),
result should be 'a' 'b';
So I define a function as below,
(defun myr (b a) (if (= CAAR(a) b) CDAR(a) 'nope myr(b CDR(a))));
and call like this
myr(b ((b a) (b c) (a d) (a f)))
But result is like variable myr has no value
Its my first time in Lisp, So just tell me what keyword should I search for will be great help for me.
Thank you for reading.
You really need to start with a good lisp book, e.g., PCL or ACL.
You will save yourself a lot of time.
Lisp syntax is different from C.
In C, you call a function f with arguments х and y like this: f(x,y).
In Lisp, you do it like this: (f x y).
When you invoke your function myr(...), you put the symbol myr in the variable position, not function position, which causes the error you reported.
You also need to use quote as appropriate.
Related
I have a question regarding trying to define a recursively defined foldl function in Racket.
Here is my approach:
(define foldl
(lambda (z c xs)
(match xs
(empty z)
((make-pair x xs) (foldl c (c z x) xs)))))
Unfortunately, when I do this, I get the Error:
expected a function after the open parenthesis but received 1
I cannot quite figure out why this message is popping up. Can someone help me?
The error you are seeing is use to swapping the arguments to foldl.
Your definition is:
(define foldl
(lambda (z c xs)
Here z is an element a c is a constructor.
In
(foldl c (c z x) xs)))))
you swapped the first two argument.
Note: You need to change the match patterns.
Change empty to '().
Change make-pair to cons.
Say i send this code to the clisp interpreter:
(values 'a 'b)
A ;
B
Now i wish to record this information (using a setf) and to access both A and B from the results.
How can i, later, access both the values there?
I tried doing:
(setf result (values 'a 'b))
A
It only stores 'A and not 'B. How can i do this?
As you can see, functions in lisp can return multiple values via the values form. If you want to bind the values to variables, you can use multiple-value-bind. Example
(multiple-value-bind (a b c) (values 1 2 3) (+ a b c))
1 will be bound to a, 2 will be bound to b, 3 will be bound to c, and so the result of evaluating the form will be 6. If you tried to setf a values form to a var, then only the first value will be used.
I'm am looking for a macro that does effectively the opposite of this maybe-m. That is, I want the result of the first expression that returns non-nil without evaluating the remaining expressions. This last part is important because if the "solution" uses lazy-sequences then they will be evaluated in chunks...meaning the expressions could be evaluated when I don't want them to.
Here's an example of what I'm looking for:
(defn really-cool-stuff [a b c]
(first-not-nil
(operation1 a b) ; <-- this returns nil,
(operation2 b c) ; <-- this returns non-nil, the result is returned
(operation3 a b c))) ; <-- this is not evaluated
Because nil is falsey, or will do what you are looking for.
(defn really-cool-stuff [a b c]
(or
(operation1 a b)
(operation2 b c)
(operation3 a b c)))
The only exception is if the functions could potentially return false. In that case, you can build a macro that mimics or, except with a more strict conditional.
(defmacro or-nil?
([] nil)
([x] x)
([x & next] `(let [or# ~x] (if (nil? or#) (or-nil? ~#next) or#))))
I tried defining a structure with a custom print function and constructor like so:
(defun print-test (a-test stream depth)
(format stream "#<TEST-STRUCT ~A>" (test-struct-a a-test)))
(defstruct (test-struct (:print-function print-test
:constructor create-test
(&key a (b a) c)))
a
b
c)
But on evaluation I get:
Bad defstruct option (:PRINT-FUNCTION PRINT-TEST :CONSTRUCTOR
CREATE-TEST (&KEY A B C)).
[Condition of type CCL::SIMPLE-PROGRAM-ERROR]
But specifying either keyword alone works just fine. How can I fix this?
According to the grammar, options must be parenthesized individually. The defstruct form therefore needs to look like this:
(defstruct (test-struct (:print-function print-test)
(:constructor create-test (&key a (b a) c)))
a
b
c)
I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative number. My question is: can I convert it somehow to a valid pointer? My attempts to use cffi:convert-from-foreign and cffi:translate-from-foreign (what's the difference between them anyway?) failed.
Below is my actual (not working) code:
(defun put-pixel (pixbuf x y r g b)
(let ((p (+ (gdk:pixbuf-get-pixels pixbuf) (* x (gdk:pixbuf-get-n-channels pixbuf)) (* y (gdk:pixbuf-get-rowstride pixbuf)))))
(setf (cffi:mem-aref p :unsigned-char 0) r)
(setf (cffi:mem-aref p :unsigned-char 1) g)
(setf (cffi:mem-aref p :unsigned-char 2) b)))
CFFI:TRANSLATE-FROM-FOREIGN is a generic function. You can define your own foreign types using CFFI:DEFINE-FOREIGN-TYPE and then add a method to CFFI:TRANSLATE-FROM-FOREIGN to specify how the conversions from foreign to Lisp values should work.
CFFI:CONVERT-FROM-FOREIGN is what you should call if you need to explicitly convert some value. It will call CFFI:TRANSLATE-FROM-FOREIGN behind the scenes and it might perform some compile-time optimizations if possible.
Same thing applies to CFFI:CONVERT-TO-FOREIGN and CFFI:TRANSLATE-TO-FOREIGN.
I think that lambda-gtk incorrectly defined binding for pixbuf-get-pixels.
The negative value for pointer value might appear because of incorrect interpretation of unsigned integer as a signed integer.
The simplest way to correct this value is to use mod:
CL-USER> (mod -1 (expt 2 #+cffi-features:x86 32 #+cffi-features:x86-64 64))
4294967295