TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ElpanovEvgeniy on April 22, 2009, 05:48:56 AM
-
Hello to all!
New Challenge.
function1 :
To multiply all elements of the list by 3
(setq a '((1 2 3)(4 5 6)(7 8 9)))
(test1 a) =>> '((3 6 9) (12 15 18) (21 24 27))
Explanation:
'(((* 1 3) (* 2 3) (* 3 3))
((* 4 3) (* 5 3) (* 6 3))
((* 7 3) (* 8 3) (* 9 3))
)
Example:
(test1 '((1 2 3)(4 5 6)(7 8 9))) =>> '((3 6 9) (12 15 18) (21 24 27))
(test1 '((1 2 3 4 5 6)(7 8 9)(10 5 0))) =>> '((3 6 9 12 15 18) (21 24 27) (30 15 0))
(test1 '((1)(4 5)(10 5 0))) =>> '((3) (12 15) (30 15 0))
function2 :
Addition of two lists with shift
(setq a '((1 2 3) (4 5 6) (7 8 9)))
(setq b '((11 12 13) (14 15 16) (17 18 19)))
(test2 a b) =>> '((1 13 15) (4 19 21) (7 25 27))
Explanation:
'((1 (+ 2 11) (+ 3 12))
(4 (+ 5 14) (+ 6 15))
(7 (+ 8 17) (+ 9 18))
)
Example:
(test2 '((1 2 3) (4 5 6) (7 8 9)) '((11 12 13) (14 15 16) (17 18 19)))
;=>> '((1 13 15) (4 19 21) (7 25 27))
(test2 '((1 2 3 4 5 6)(7 8 9)(10 5 0))'((11 12 13) (14 15 16) (17 18 19)))
;=>> '((1 13 15 17) (7 22 24) (10 22 18))
(test2 '((1)(4 5)(6 7 8)) '((1)(4 5)(6 7 8)))
;=>> '((1) (4 9) (6 13 15))
-
Function 1:
(defun Fun1(arg)
(mapcar
(function
(lambda(a)
(if (vl-consp a)
(fun1 a)
(* 3 a)
)
)
)
arg
)
)
And Function 2
(defun Fun2 (arg1 arg2)
(mapcar
(function
(lambda (a1 a2)
(mapcar
(function
(lambda (i1 i2)
(+ i1 i2)
)
)
a1
(append '(0) a2)
)
)
)
arg1
arg2
)
)
-
Good solution
-
Hello :)
My contribution
Function 1
(defun operation(type nombre lst)
(mapcar '(lambda(x)
(if (listp x)
(operation type nombre x)
((eval type) nombre x)
)
)
lst
)
)
(operation '* 3 '((1 2 3)(4 5 6)(7 8 9))) --> ((3 6 9) (12 15 18) (21 24 27))
(operation '* 3 '((1 2 3 4 5 6)(7 8 9)(10 5 0))) --> ((3 6 9 12 15 18) (21 24 27) (30 15 0))
(operation '* 3 '((1)(4 5)(10 5 0))) --> ((3) (12 15) (30 15 0))
(operation '+ 11 '((1 2 3)(4 5 6)(7 8 9))) --> ((12 13 14) (15 16 17) (18 19 20))
Function 2
(defun shift(type lst1 lst2)
(mapcar '(lambda(x y)
(if (listp x)
(cons (car x) (shift type (cdr x) y))
((eval type) x y)
)
)
lst1
lst2
)
)
(shift '+ '((1 2 3)(4 5 6)(7 8 9))'((11 12 13)(14 15 16)(17 18 19))) --> ((1 13 15) (4 19 21) (7 25 27))
(shift '+ '((1 2 3 4 5 6)(7 8 9)(10 5 0))'((11 12 13)(14 15 16)(17 18 19))) --> ((1 13 15 17) (7 22 24) (10 22 18))
(shift '+ '((1)(4 5)(6 7 8)) '((1)(4 5)(6 7 8))) --> ((1) (4 9) (6 13 15))
(shift '* '((1 2 3)(4 5 6)(7 8 9))'((11 12 13)(14 15 16)(17 18 19))) --> ((1 22 36) (4 70 90) (7 136 162))
@+
-
Good universal solution!
My variant, not universal.
But shorter.
-
Knowing ElpanovEvgeniy, I know in advance that it is very difficult or impossible to make shorter than him. :-D
I have proposed a recursive solution to also address complex lists. 8-)
(shift '+ '((1 2 (3 4) 5 6)(7 8 9)(10 5 0))'((11 (12) 13)(14 15 16)(17 18 19))) --> ((1 13 (3 16) 18) (7 22 24) (10 22 18))
@+
-
saving a couple of bytes of my traffic :)
(defun test1 (a)
(mapcar '(lambda (a) (mapcar '* a '(3 3 3))) a)
)
(defun test2 (a b)
(mapcar '(lambda (a b) (cons (car a) (mapcar '+ (cdr a) b))) a b)
)
-
Here is my contribution.
(defun MathList ( mathFunction listIn num )
(mapcar
(function
(lambda (x)
(cond
((atom x)
(mathFunction x num)
)
((listp x)
(MathList mathFunction x num)
)
)
)
)
listIn
)
)
Now to see how others did it. :-D
Edit: Here is the second one. Didn't read well enough the first post.
(defun test2 ( listIn1 listIn2 )
(mapcar
(function
(lambda ( a b )
(cons
(car a)
(mapcar
(function
(lambda ( x y )
(+ x y)
)
)
(cdr a)
b
)
)
)
)
listIn1
listIn2
)
)
-
or maybe.....
(defun multby3 (n) (* n 3))
(mapcar '(lambda (sub) (mapcar 'multby3 sub)) lst)
or another maybe...
(mapcar (function (lambda (sub) (mapcar (function (lambda (n) (* n 3))) sub))) lst)
edit: i guess did not read the whole enchilada of what's needed, if i had a chance later will try to collaborate /:
-
Python just for fun ...
Given some variables:
a=1
b=(1, 2, 3)
c=((1, 2, 3), (4, 5, 6), (7, 8, 9))
A simple function:
def foo(lst, multiplier=1):
if type(lst) in (list, tuple):
return [foo(i, multiplier) for i in lst]
else:
return lst*multiplier
Applied:
foo(a, 3) => 3
foo(b, 3) => [3, 6, 9]
foo(c, 3) => [[3, 6, 9], [12, 15, 18], [21, 24, 27]]
Now for something completely different, a functor (complete overkill in this context, but for fun show its construct and use):
A simple class with a call method:
class Foo():
def __init__(self, multiplier=1):
self.multiplier=multiplier
def __call__(self, lst):
if type(lst) in (list, tuple):
return [self.__call__(i) for i in lst]
else:
return lst*self.multiplier
Create the functor and apply it:
foo = Foo(3)
foo(a) => 3
foo(b) => [3, 6, 9]
foo(c) => [[3, 6, 9], [12, 15, 18], [21, 24, 27]]
Finally, replicate a functor effect via a closure (again, overkill, posting for illustration only):
def make_foo(multiplier):
def foo(lst):
if type(lst) in (list, tuple):
return [foo(i) for i in lst]
else:
return lst*multiplier
return foo
Use the closure:
foo = make_foo(3)
foo(a) => 3
foo(b) => [3, 6, 9]
foo(c) => [[3, 6, 9], [12, 15, 18], [21, 24, 27]]
Carry on ... :D
-
Python just for fun ...
I was beginning to think that no one would offer other-language-solutions (and I didn't want to be the first).
But, now that MP has broken that ice, I will offer my elementary Ruby solution :-D
##Extend the Array class
class Array
def mult(arg)
self.map{|x| x.map{|y| y*arg}}
end
end
a =[[1,2,3,4],[4,5,6,7],[7,8,9,10]]
d = a.mult(3)
puts d.inspect
returns [[3, 6, 9, 12], [12, 15, 18, 21], [21, 24, 27, 30]]
-
Very cool David. :)
What happens on a numeric literal or a non nested array? eg:
a=1
d=a.mult(3) ## I'm assuming *crash*, not an array so method not available
puts d.inspect
??
-- or --
a=[1,2,3]
d=a.mult(3)
puts d.inspect
??
-
PS: foo as defined in the preceding python post as a simple function will properly process this mess:
foo((1,(2,(3,(4,(5,(6,(7,(8,)))))))),3)
[3, [6, [9, [12, [15, [18, [21, [24]]]]]]]]
:D
-
F#:
using F# interactive:
> let alist = [[1;2;3];[4;5;6];[7;8;9]]
let times3 x = 3*x
(List.map (fun x -> List.map times3 x) alist);;
val it : int list list = [[3; 6; 9]; [12; 15; 18]; [21; 24; 27]]
>
of course a named function is unnecessary.
> (List.map (fun x -> List.map (fun x -> 3*x) x) alist);;
val it : int list list = [[3; 6; 9]; [12; 15; 18]; [21; 24; 27]]
>
-
My version...
(defun test1 (a) (mapcar '(lambda (a) (mapcar '+ a a a)) a))
(defun test2 (a b) (mapcar '(lambda (a b) (mapcar '+ a (cons 0 b))) a b))
-
I started my journey into functional programming a couple of weeks ago and started trolling the LISP groups to help revive some long dormant grey cells. I stumbled across this challenge early this week and was thrilled to see all the multi-language replies, especially Herman's, since F# is the FP language that I'm starting with. I'm glad to know that there's at least one familiar person that I can turn to :-)
Here's my F# one liner submission. It's the same as Herman's; it just uses currying and forward piping to clean things up a bit.
[[0..9];[10..19];[20..29]] |> List.map (List.map (fun n -> n*3));;
Which returns
[[0; 3; 6; 9; 12; 15; 18; 21; 24; 27];
[30; 33; 36; 39; 42; 45; 48; 51; 54; 57];
[60; 63; 66; 69; 72; 75; 78; 81; 84; 87]]
Although in real code I'd name it and allow any function to be passed in.
let nestedMap f inputList =
inputList |> List.map (List.map f)
Anyway, great post, great replies. I sure hope to see more.
-
http://through-the-interface.typepad.com/through_the_interface/f/index.html
http://www.manning.com/petricek/
http://www.apress.com/book/search?searchterm=f%23&act=search&submit.x=0&submit.y=0
http://www.apress.com/book/search?searchterm=f%23&act=search&submit.x=0&submit.y=0