Jump to content

Call a function in mapcar lambda function


Shakto

Recommended Posts

Hello,

 

i've a question, i made 9 function (g1 g2 g3 m1 m2 m3 p1 p2 p3)

I want to call each of them in a mapcar function, the 9 functions are into a list in arg of my function :

 

Let see exemple of g1

(defun g1 (l) (if (eq (car l) 1) NIL (cons 1(cdr l))))

 

My list of function : (g1 g2 g3 m1 m2 m3 p1 p2 p3)

 

When i want to call them for a value in that function, it's not working, someone can help me please ?)

 

(defun newopen (felem lop) (mapcar (lambda (y) (y felem)) lop))
(newopen '(1 2 3) '(g1 g2 g3 m1 m2 m3 p1 p2 p3))

Edited by Shakto
Link to comment
Share on other sites

 

When i want to call them for a value in that function, it's not working, someone can help me please ?)

 

(defun newopen (felem lop) (mapcar (lambda (y) (y felem)) lop))

(newopen '(1 2 3) '(g1 g2 g3 m1 m2 m3 p1 p2 p3))

 

hi shako, welcome to forum, read this 1st

http://www.cadtutor.net/forum/showthread.php?9184-Code-posting-guidelines

 

maybe typo error ?

lambda quote missing?

[color="red"][b]'[/b][/color](lambda...)

 

also

if list=
'(g1 g2 g3 m1 m2 m3 p1 p2 p3) ;
you need, (([color="red"]eval[/color] y) felem)

or just
([color="red"]list[/color] g1 g2 g3 m1 m2 m3 p1 p2 p3)

we don't know how the rest functions g2 g3 m2...etc working or have bugs?

Link to comment
Share on other sites

Didn't know about the eval function, still not working with the function newopen :

(defun newopen (felem lop) (mapcar '(lambda (y) ((eval y) felem)) lop))

 

I have that bug error : FUNCALL : The argument (lambda y) ((eval y) felem)) isn't a function

 

The others functions g1 g2 g3 etc... are working :

(defun g1 (l) (if (eq (car l) 1) NIL (cons 1(cdr l))))
(defun g2 (l) (if (eq (car l) 2) NIL (cons 2(cdr l))))
(defun g3 (l) (if (eq (car l) 3) NIL (cons 3(cdr l))))
(defun m1 (l) (if (eq (car l) 1) NIL (if (eq (cadr l) 1) NIL (if  (eq (car l) (cadr l)) NIL (cons (car l) (cons 1 (cddr l)))))))
(defun m2 (l) (if (eq (car l) 2) NIL (if (eq (cadr l) 2) NIL (if  (eq (car l) (cadr l)) NIL (cons (car l) (cons 2 (cddr l)))))))
(defun m3 (l) (if (eq (car l) 3) NIL (if (eq (cadr l) 3) NIL (if  (eq (car l) (cadr l)) NIL (cons (car l) (cons 3 (cddr l)))))))
(defun p1 (l) (if (eq (car l) 1) NIL (if (eq (caddr l) 1) NIL (if (eq (car l) (caddr l)) NIL (if (eq (cadr l) (caddr l)) NIL (append (list(car l)) (list(cadr l)) (cons 1 ())))))))
(defun p2 (l) (if (eq (car l) 2) NIL (if (eq (caddr l) 2) NIL (if (eq (car l) (caddr l)) NIL (if (eq (cadr l) (caddr l)) NIL (append (list(car l)) (list(cadr l)) (cons 2 ())))))))
(defun p3 (l) (if (eq (car l) 3) NIL (if (eq (caddr l) 3) NIL (if (eq (car l) (caddr l)) NIL (if (eq (cadr l) (caddr l)) NIL (append (list(car l)) (list(cadr l)) (cons 3 ())))))))

Link to comment
Share on other sites

without eval

(defun newopen1 (felem lop) (mapcar '(lambda (y) ([color="red"]y[/color] felem)) lop))
(newopen1 '(1 2 3) ([color="blue"]list[/color] g1 g2 g3 m1 m2 m3 p1 p2 p3))

 

with eval:

(defun newopen2 (felem lop) (mapcar '(lambda (y) (([color="blue"][b]eval[/b][/color] y) felem)) lop))
(newopen2 '(1 2 3) [color="red"][b]'[/b][/color](g1 g2 g3 m1 m2 m3 p1 p2 p3))

This may explain: QUOTE

 

This is your return value?

;(nil (2 2 3) (3 2 3) nil nil (1 3 3) nil (1 2 2) nil) 

Link to comment
Share on other sites

Yes it's my return value.

 

I have errors with both of your function :

 

newopen1 => Error: The variable G1 is unbound.

newopen2 => Argument to apply/funcall is not a function: (LAMBDA (Y) ((EVAL Y) FELEM)).

Link to comment
Share on other sites

Yes it's my return value.

 

I have errors with both of your function :

 

newopen1 => Error: The variable G1 is unbound.

newopen2 => Argument to apply/funcall is not a function: (LAMBDA (Y) ((EVAL Y) FELEM)).

 

maybe after version 2007 lambda needs to be optimized though without compile ?? :?

(defun newopen2 (felem lop) (mapcar ([color="blue"]function[/color](lambda (y) ((eval y) felem))) lop))

(newopen2 '(3 2 1) '(g1 g2 g3 m1 m2 m3 p1 p2 p3))
;((1 2 1) (2 2 1) nil (3 1 1) nil nil nil (3 2 2) nil) 

Link to comment
Share on other sites

As hanhphuc has suggested, the following is correct:

(defun newopen ( felem lop ) (mapcar '(lambda ( y ) ((eval y) felem)) lop))

In my testing:

_$ (newopen '(1 2 3) '(g1 g2 g3 m1 m2 m3 p1 p2 p3))
(nil (2 2 3) (3 2 3) nil nil (1 3 3) nil (1 2 2) nil)

 

What software/method are you using to evaluate your code?

Are you entering the code at the AutoCAD 2014 command-line or in the VLIDE console?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...