(mapcar '+ '(0.0 200.0 0.0) <list-item>)
(defun myfunction ( x )
(mapcar '+ '(0.0 200.0 0.0) x)
)
(lambda ( x ) (mapcar '+ '(0.0 200.0 0.0) x))
Think of the lambda function simply as a function defined without a function name - an anonymous function.(mapcar 'myfunction <list>)
(mapcar '(lambda ( x ) (mapcar '+ '(0.0 200.0 0.0) x)) <list>)
Code - Text: [Select]Here mapcar is supplied with the + function, which takes any number of numerical arguments and adds them together. Hence in the above example, mapcar will evaluate the + function on each member of each list and return a list of the results:
(mapcar '+ '(1 2 3 4 5) '(3 4 5 6 7)) (4 6 8 10 12)Code - Text: [Select]
(4 6 8 10 12) = ((+ 1 3) (+ 2 4) (+ 3 5) (+ 4 6) (+ 5 7))
mapcar is a specialized foreach function
defined as
(mapcar function list1... listn)
Lambda: defined as
(lambda arguments expr...)Code - Auto/Visual Lisp: [Select]mapcar feeds each value from the list (2 4 -6 10.2) to x then collects the return values in a list
the (setq counter (1+ counter)) counts the number of times the lambda was executed which will be 4
this also illustrates that more than one line of code may be used within the lambda function
the ' symbol could be replaced with (function and would perform better (faster).
the (* x 5) process the x variable as follows
(* 2 5) 10
(* 4 5) 20
(* -6 5) -30
(* 10.2 5) 51
so it return
(10 20 -30 51.0)
Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.MIT so, no answers, this is what I came up with
(defun summed ( x1 x2 x3 / n1 n2 data)
(setq n1 (if (> x1 x2)
(* x1 x1)
(* x2 x2)
);if
);setq
(setq n2 (if (> x1 x3)
(* x1 x1)
(* x3 x3)
);if
);setq
(setq data (+ n1 n2))
)
Quick paste into cad and i'm pleasantly being returned these:(_> (setq data (+ n1 n2)))
SUMMED
Command: (summed 1 2 3)
13
Command: (summed 2 4 3)
25
(setq pt1 '(1.5 2.5 3.5))
(setq pt2 '(2.0 3.0 4.0))
(setq pt3 '(3.5 4.5 5.5))
(setq lst '(pt1 pt2 pt3) )
(defun getx ( pt /)
(if pt
(princ (car pt))
)
(princ)
)
(mapcar
'(lambda ( pt ) (if pt (princ (car pt)))
lst
)
Fails. Miserably. I thought that would work, when substituting lambda for the function that works if I do it (getx pt1) with those points defined.QuoteExercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.MIT so, no answers, this is what I came up withCode: [Select](defun summed ( x1 x2 x3 / n1 n2 data)
(setq n1 (if (> x1 x2)
(* x1 x1)
(* x2 x2)
);if
);setq
(setq n2 (if (> x1 x3)
(* x1 x1)
(* x3 x3)
);if
);setq
(setq data (+ n1 n2))
)
_$ (summed 1 2 3)
13
_$ (summed 3 2 1)
18
(defun sumsqr ( x y z / l )
(setq l (list x y z)
l (mapcar '* l l)
)
(apply 'max (mapcar '+ (cons (last l) l) l))
)
I'll have to take them symbolically in order to interchange the arithmetic with useful programming skills, for instanceCode: [Select](setq pt1 '(1.5 2.5 3.5))
Fails. Miserably. I thought that would work, when substituting lambda for the function that works if I do it (getx pt1) with those points defined.
(setq pt2 '(2.0 3.0 4.0))
(setq pt3 '(3.5 4.5 5.5))
(setq lst '(pt1 pt2 pt3) )
(defun getx ( pt /)
(if pt
(princ (car pt))
)
(princ)
)
(mapcar
'(lambda ( pt ) (if pt (princ (car pt)))
lst
)
(setq lst '(pt1 pt2 pt3))
QuoteExercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
(defun sumsqr ( x y z / m) ;define
(setq m (min x y z)) ;sets a variable m to be equal to the lowest of the three values
(cond ((= x m) (+ (* y y) (* z z))) ;checks if x is lowest, thereby qualifying y and z, add the sqrts
((= y m) (+ (* x x) (* z z))) ;checks if y is lowest, thereby qualifying x and z, add the sqrts
((+ (* x x) (* y y))) ;otherwise z must be lowest therefor x and y are qualified, add the sqrts
) ;cond
) ;defun
Another for fun:Code - Auto/Visual Lisp: [Select]
(defun sumsqr ( x y z / l ) ;define, 3 arguments
(apply ;apply the following
'+ ;add the results together
(mapcar ;do the following function to each item in the following list
'* ;telling the mapcar to multiply the values found in the following list by the values of the supplied list
(setq l (cdr
(vl-sort (list x y z) '<) ;seems like cheating but you used an active-x vl-sort, gave it the list, then told it what to sort by "less than" this places the smallest value in the
;first position in the list, which is why you call (cdr) in the next nested statement, this in effect "finds" the two greatest numbers. Cheater.
)
);set a variable 'l' to contain the two larger numbers
l ;2nd argument to mapcar, the list to be multiplied BY. These two lists are the same. This squares them
) ;the nesting makes it complicated but obviously the (apply '+ (* l l)) has the squares added together.
)
Code: [Select]Cheater.
well, obviously I've still got some learning to do but I think i'm getting there. A lot further than I was this morning, that's for sure.
Heading out for the day in a few minutes, enjoy your weekend everyone.
Oh and I was just kidding Lee (about the cheating), your ability to provide alternative methods of accomplishing the same task is as usual, very impressive.
One day I hope to have as great an understanding as you do.