Author Topic: -={ Challenge }=- Subst at n  (Read 7275 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
-={ Challenge }=- Subst at n
« on: March 28, 2010, 06:38:57 PM »
The Challenge: to substitute an item in a list at a specified position.

Example:

Code: [Select]
(subst_at_n 2 '(0 1 2 3 4 5) 4)

=>  (0 1 2 3 2 5)


I figure there are quite a few ways to do this, so the challenge might be interesting.

I apologise in advance if this has been addressed in the past - I did do a search, but was perhaps not thorough enough.

My entry:

Code: [Select]
 (defun Subst_at_n (item lst n)
    (if lst
      (cons (if (zerop n) item (car lst))
            (Subst_at_n item (cdr lst) (1- n)))
      (car lst)))
« Last Edit: March 29, 2010, 06:18:30 AM by Lee Mac »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Subst at n
« Reply #1 on: March 28, 2010, 06:49:22 PM »
I wrote this back in June...

Code: [Select]
;;; Replace nth item in list
;;; #Nth - nth number in list to replace
;;; #New - replacement item
;;; #List - list to process
;;; Alan J. Thompson, 06.16.09
(defun AT:NthReplace (#Nth #New #List / #Count)
  (setq #Count -1)
  (mapcar '(lambda (x)
             (if (eq #Nth (setq #Count (1+ #Count)))
               #New
               x
             ) ;_ if
           ) ;_ lambda
          #List
  ) ;_ mapcar
) ;_ defun

This would be better...
Code: [Select]
(defun AT:NthReplace (#Nth #New #List / #Count)
  (if (zerop #Nth)
    (cons #New (cdr #List))
    (progn (setq #Count -1)
           (mapcar (function (lambda (x)
                               (if (eq #Nth (setq #Count (1+ #Count)))
                                 #New
                                 x
                               ) ;_ if
                             ) ;_ lambda
                   ) ;_ function
                   #List
           ) ;_ mapcar
    ) ;_ progn
  ) ;_ if
) ;_ defun
« Last Edit: March 29, 2010, 11:58:35 AM by alanjt »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Subst at n
« Reply #2 on: March 28, 2010, 07:05:06 PM »
BTW Lee...

Code: [Select]
([color=red]Mac-[/color]Subst_at_n item (cdr lst) (1- n)))
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #3 on: March 28, 2010, 07:12:09 PM »
Wowza... I made that change in the post box, something I'm never going to do again... Thanks alan

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Subst at n
« Reply #4 on: March 28, 2010, 07:15:40 PM »
Wowza... I made that change in the post box, something I'm never going to do again... Thanks alan

:) I've done the exact same thing.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: -={ Challenge }=- Subst at n
« Reply #5 on: March 28, 2010, 07:32:31 PM »
Code: [Select]
;; variation by CAB
(defun replace_CAB (lst i itm)
  (setq i (1+ i))
  (mapcar '(lambda (x) (if (zerop (setq i (1- i))) itm x)) lst)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #7 on: March 28, 2010, 07:36:13 PM »
Quick & dirty & just for fun ...

Code: [Select]
(defun _ReplaceNth ( n item lst / f )

    (defun f ()
        (if (zerop n)
            (progn (defun f () x) item)
            (progn (setq n (1- n)) x)
        )    
    )            
        
    (mapcar (function (lambda (x) (f))) lst)
        
)
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 }=- Subst at n
« Reply #8 on: March 28, 2010, 07:39:25 PM »
and for even more fun ...

Code: [Select]
(defun _ReplaceNth ( n item lst )
    (   (lambda (f) (mapcar (function (lambda (x) (f))) lst))
        (lambda ( )
            (if (zerop n)
                (progn (defun f () x) item)
                (progn (setq n (1- n)) x)
            )    
        )            
    )
)

 :lol:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: -={ Challenge }=- Subst at n
« Reply #9 on: March 28, 2010, 07:50:39 PM »
Code: [Select]
#include "stdafx.h"
#include <array>
#include <iostream>

using namespace std;
using namespace std::tr1;

int _tmain(int argc, _TCHAR* argv[])
{
  array<int,9> ints = {1,2,3,4,5,6,7,8,9};
  ints[2] = 9;
  for_each(ints.begin(), ints.end(), [](int n) { cout << n << " "; });
  cout << endl;
  system("pause");
  return 0;
}
« Last Edit: March 28, 2010, 07:56:26 PM by Daniel »

LE3

  • Guest
Re: -={ Challenge }=- Subst at n
« Reply #10 on: March 28, 2010, 07:55:03 PM »
Be careful don't end like me and be tagged for ever and be part of a real facts...  :lol:  :evil:  :lmao:

Here is something from my very old lisp coding school.-
Code: [Select]
(defun substAtPosition  (newItem position oldList / cont lth lst)
  (setq cont 0
lth  (length oldList))
  (while (/= cont position)
    (setq lst (append lst (list (nth cont oldList)))
 cont (1+ cont)))
  (setq lst (append lst (list newItem))
cont (1+ cont))
  (while (< cont lth)
    (setq lst (append lst (list (nth cont oldList)))
 cont (1+ cont)))
  lst)
 
Quote
(substAtPosition 2 4 '(0 1 2 3 4 5))
(0 1 2 3 2 5)

Wowza... I made that change in the post box, something I'm never going to do again... Thanks alan


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- Subst at n
« Reply #11 on: March 28, 2010, 10:36:52 PM »

Quote
int _tmain(int argc, _TCHAR* argv[])

sick puppy  :-P
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: -={ Challenge }=- Subst at n
« Reply #12 on: March 28, 2010, 11:41:46 PM »
I thought you might like the lambda expression, I can format it like lisp  :laugh:

Code: [Select]
for_each(
 ints.begin(),
    ints.end(),
        [](int n) { cout << n << " "; });

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #13 on: March 29, 2010, 06:20:27 AM »
Nice one Michael - I love that 'double-defun' trick  :lol:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #14 on: March 29, 2010, 06:22:28 AM »
http://www.theswamp.org/index.php?topic=14170.0


I knew there would be another one, there was no way this operation was untouched  :evil: