Author Topic: (Challenge) mapcar  (Read 4112 times)

0 Members and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
(Challenge) mapcar
« on: April 22, 2009, 05:48:56 AM »
Hello to all!
New Challenge.

function1 :
To multiply all elements of the list by 3

Code: [Select]
(setq a '((1 2 3)(4 5 6)(7 8 9)))
(test1 a) =>> '((3 6 9) (12 15 18) (21 24 27))

Explanation:

Code: [Select]
'(((* 1 3) (* 2 3) (* 3 3))
  ((* 4 3) (* 5 3) (* 6 3))
  ((* 7 3) (* 8 3) (* 9 3))
  )

Example:
Code: [Select]
(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

Code: [Select]
(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:

Code: [Select]
'((1 (+ 2 11) (+ 3 12))
  (4 (+ 5 14) (+ 6 15))
  (7 (+ 8 17) (+ 9 18))
  )

Example:
Code: [Select]
(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))

mkweaver

  • Bull Frog
  • Posts: 352
Re: (Challenge) mapcar
« Reply #1 on: April 22, 2009, 07:11:19 AM »
Function 1:
Code: [Select]
(defun Fun1(arg)
  (mapcar
    (function
      (lambda(a)
(if (vl-consp a)
  (fun1 a)
  (* 3 a)
  )
)
      )
    arg
    )
  )

And Function 2
Code: [Select]
(defun Fun2 (arg1 arg2)
  (mapcar
    (function
      (lambda (a1 a2)
(mapcar
  (function
    (lambda (i1 i2)
      (+ i1 i2)
    )
  )
  a1
  (append '(0) a2)
)
      )
    )
    arg1
    arg2
  )
)



ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) mapcar
« Reply #2 on: April 22, 2009, 07:22:43 AM »
Good solution

Patrick_35

  • Guest
Re: (Challenge) mapcar
« Reply #3 on: April 22, 2009, 08:03:11 AM »
Hello :)

My contribution

Function 1
Code: [Select]
(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
Code: [Select]
(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))

@+

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) mapcar
« Reply #4 on: April 22, 2009, 08:17:40 AM »
Good universal solution!

My variant, not universal.
But shorter.

Patrick_35

  • Guest
Re: (Challenge) mapcar
« Reply #5 on: April 22, 2009, 08:48:11 AM »
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))

@+

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) mapcar
« Reply #6 on: April 22, 2009, 11:07:10 AM »
saving a couple of bytes of my traffic :)
Code: [Select]
(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)
)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: (Challenge) mapcar
« Reply #7 on: April 22, 2009, 11:31:14 AM »
Here is my contribution.

Code: [Select]
(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.

Code: [Select]
(defun test2 ( listIn1 listIn2 )
   
    (mapcar
        (function
            (lambda ( a b )
                (cons
                    (car a)
                    (mapcar
                        (function
                            (lambda ( x y )
                                (+ x y)
                            )
                        )
                        (cdr a)
                        b
                    )
                )
            )
        )
        listIn1
        listIn2
    )
)
« Last Edit: April 22, 2009, 11:42:20 AM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Spike Wilbury

  • Guest
Re: (Challenge) mapcar
« Reply #8 on: April 22, 2009, 11:47:56 AM »
or maybe.....

Code: [Select]
(defun multby3 (n) (* n 3))
(mapcar '(lambda (sub) (mapcar 'multby3 sub)) lst)

or another maybe...
Code: [Select]
(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 /:
« Last Edit: April 22, 2009, 12:14:51 PM by Luis »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (Challenge) mapcar
« Reply #9 on: April 22, 2009, 02:55:44 PM »
Python just for fun ...

Given some variables:
Code: [Select]
a=1
b=(1, 2, 3)
c=((1, 2, 3), (4, 5, 6), (7, 8, 9))

A simple function:
Code: [Select]
def foo(lst, multiplier=1):
    if type(lst) in (list, tuple):
        return [foo(i, multiplier) for i in lst]
    else:
        return lst*multiplier

Applied:       
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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):
Code: [Select]
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:
Code: [Select]
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
« Last Edit: April 22, 2009, 03:47:54 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

SomeCallMeDave

  • Guest
Re: (Challenge) mapcar
« Reply #10 on: April 22, 2009, 03:51:23 PM »
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

Code: [Select]
##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]]

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (Challenge) mapcar
« Reply #11 on: April 22, 2009, 04:00:19 PM »
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
??
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (Challenge) mapcar
« Reply #12 on: April 22, 2009, 04:05:23 PM »
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
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

hermanm

  • Guest
Re: (Challenge) mapcar
« Reply #13 on: April 22, 2009, 11:20:21 PM »
F#:
using F# interactive:

Code: [Select]
> 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.

Code: [Select]

> (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]]
>


ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) mapcar
« Reply #14 on: April 23, 2009, 02:07:47 PM »
My version...

Code: [Select]
(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))

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: (Challenge) mapcar
« Reply #15 on: May 01, 2009, 02:38:13 PM »
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.
Code: [Select]
[[0..9];[10..19];[20..29]] |> List.map (List.map (fun n -> n*3));;

Which returns
Code: [Select]
[[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.
Code: [Select]
let nestedMap f inputList =
  inputList |> List.map (List.map f)

Anyway, great post, great replies.  I sure hope to see more.
Bobby C. Jones