MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: match 1, replace 2


I would use functional programming rather then pattern matching (rules)
Define a function f as the following
f[l_List] := Flatten[{Map[{100 + #, -1} &, Most[l]], Last[l]}]
and then Map[] it over ls
Map[f,ls]
and get 
{{101, -1, 102, -1, 3}, {104, -1, 5}, {106, -1, 107, -1, 108, -1, 9}}
as you needed
yehuda


On 4/19/05, Trevor Baca <trevorbaca at gmail.com> wrote:
> hi mathgroup,
> 
> what rule takes
> 
> ls = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}}
> 
> to
> 
>       {{101, -1, 102, -1, 3}, {104, -1, 5}, {106, -1, 107, -1, 108, -1,
> 9}}
> 
> ie, matches all integers but the last in each list and then replaces
> each matched integer n with the 2-element sequence Sequence[100 + n,
> -1]?
> 
> trevor.
> 
>


  • Prev by Date: Re: match 1, replace 2
  • Next by Date: Re: Eigensystem Bug? OS-X
  • Previous by thread: match 1, replace 2
  • Next by thread: Re: match 1, replace 2