2
$\begingroup$

I used all my google-fu trying to find how to make this work; read the docs as well. What have I missed? I feel sure this can't be a new question, but I can't find anything Here is is:

Take[#1, {#2, Length[#1]}] &[V3, 3]

V3 is a List. This above works.

When I do this (someInteger is an unassigned integer variable):

Take[#1, {#2, Length[#1]}] &[V3, someInteger]

I get this error:

Sequence specification (+n, -n, {+n}, {-n}, {m, n}, or {m, n, s}) expected at position 2.

If I only define the pure function (don't pass arguments in [ ]) then it's OK - until the first attempt to use that pure function.

Is this something that should be solved by appropriate use of Evaluate?

Seems List "Takes" are evaluated only with actual numbers and not variables.

EDIT: I am trying to take a successively smaller piece of the List (it's an exercise from a book I'm using).

V3={1,2,3,4,5,6,7,8}

I want to use a pure function to get lists as follows: 1....8 2....8 3....8

It's a trivial exercise using Table, but the apparent difficulty of doing it with a pure function has me puzzled - I'm sure I'm missing something.

V3 = Table[mm, {mm, 1, 5}]

Table[V3[[j ;;]], {j, 1, 5}]

result is  {{1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5}}
$\endgroup$
  • $\begingroup$ I can't give it specific integers because I need the pure function to accept substitution values $\endgroup$ – Paul_A Feb 16 '15 at 5:54
  • 2
    $\begingroup$ why not simply V3[[# ;;]] & /@ Range[Length[V3]]? $\endgroup$ – Algohi Feb 16 '15 at 6:27
  • $\begingroup$ because i don't always want to use V3. That's why I have V3 as an argument for the pure function. The core question I am looking to answer is "how do you use pure-function arguments in Take/equivalent. This does not work: V3[[# ;;]] &[jj] - it only works with hardcoded integer-values for jj $\endgroup$ – Paul_A Feb 16 '15 at 18:02
  • $\begingroup$ You cloud define a function like this: f[list_] := list[[# ;;]] & /@ Range[Length[list]] and then use any list to get the result. f[V3] $\endgroup$ – Algohi Feb 16 '15 at 18:57
  • $\begingroup$ you can also define pure function like this g := Function[{list}, list[[# ;;]] & /@ Range[Length[list]]]. and then g[V3] $\endgroup$ – Algohi Feb 16 '15 at 19:00
5
$\begingroup$

You say you need a pure function to accept substitution values. My interpretation of this would be that you want the following:

t = Take[#1, {#2, Length[#1]}] &[V3, #] &

This defines t as a function that accepts the values as follows:

t /@ Range[8]

(*
==> {{1, 2, 3, 4, 5, 6, 7, 8}, {2, 3, 4, 5, 6, 7, 8}, {3, 4, 5, 6,
   7, 8}, {4, 5, 6, 7, 8}, {5, 6, 7, 8}, {6, 7, 8}, {7, 8}, {8}}
*)

The slot # at the end is filled with the "substitution value", whereas the slot #1 in the first part (which is taken without modification from your original definition) is filled with V3.

Sometimes cascades of # and & denoting pure functions can be a little hard to read. In that case, it could be better to use Function.

$\endgroup$
3
$\begingroup$

You can do it by nesting pure functions.

r = Range[6];
f = Function[i, Part[#, i ;;]] &;
f[r][3]
{3, 4, 5, 6}
f[r] /@ Range[Length[r]]
{{1, 2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {3, 4, 5, 6}, {4, 5, 6}, {5, 6}, {6}}
$\endgroup$
2
$\begingroup$

This is a rather weird question because don't give any explanation of why you are trying to do this the way you are. The obvious reply is that if this is trivial with Table why not use Table? Other natural Mathematica approaches include Algohi's comment:

V3[[# ;;]] & /@ Range[Length[V3]]

Or my preference, with Array:

V3[[# ;;]] & ~Array~ Length[V3]

If you wish to make this operation a function you can use standard function definition syntax:

foo[v_List] := v[[# ;;]] & ~Array~ Length[v]

Now:

foo[{1, 2, 3, 4}]
{{1, 2, 3, 4}, {2, 3, 4}, {3, 4}, {4}}

If for some reason you want this as an anonymous function rather than foo you could use:

v \[Function] v[[# ;;]] & ~Array~ Length[v]

See \[Function] for reference.

This also may interest you:

NestWhileList[Rest, V3, Length@# > 1 &]
{{1, 2, 3, 4, 5, 6, 7, 8}, {2, 3, 4, 5, 6, 7, 8}, {3, 4, 5, 6, 7, 8},
 {4, 5, 6, 7, 8}, {5, 6, 7, 8}, {6, 7, 8}, {7, 8}, {8}}
$\endgroup$
  • $\begingroup$ The reason for the apparent weirdness is that I specifically want to get experience/knowledge of List-extraction using pure functions. A wizard like yourself looks at this from the perspective of knowing a great deal - I'm trying to build knowledge by looking at fundamental aspects in detail using apparently synthetic problems/statements. Now that I know how to do with Table, I want to learn pure-function ways to do the same thing. I would upvote if I had the fu to do so $\endgroup$ – Paul_A Feb 16 '15 at 18:08
  • $\begingroup$ @user192127 That's perfectly fine. I encourage you to include such an explanation in future questions as it makes it a lot easier to provide a useful answer. By the way I think this may interest you: (7999) $\endgroup$ – Mr.Wizard Feb 16 '15 at 18:52
  • $\begingroup$ Thanks - that 7999 URL is interesting and relevant; favorited for re-reading $\endgroup$ – Paul_A Feb 17 '15 at 2:19
1
$\begingroup$

Take[#1, {#2, Length[#1]}] &[V3, someInteger] is equivalent to Take[V3, {someInteger, Length[V3]}]. But, Take[list,{m,n}] expects both m and n to be specific integers. Hence, the error message. Perhaps, you could explain what you are trying to accomplish.

With respect to edit

Is this what you had in mind?

f[list_, someInteger_] :=  NestList[Take[#1, {someInteger, Length[#1]}] &, list, 
  Round[Length[list]/(someInteger - 1)] - 1]

or, equivalently,

f = NestList[(Take[#1, {l, Length[#1]}] &) /. l -> #2, #1, Round[Length[#1]/(#2 - 1)] - 1] &

f[Range[1, 8], 2]    
(* {{1, 2, 3, 4, 5, 6, 7, 8}, {2, 3, 4, 5, 6, 7, 8}, {3, 4, 5, 6, 7, 
  8}, {4, 5, 6, 7, 8}, {5, 6, 7, 8}, {6, 7, 8}, {7, 8}, {8}} *)

f[Range[1, 8], 4]
(* {{1, 2, 3, 4, 5, 6, 7, 8}, {4, 5, 6, 7, 8}, {7, 8}} *)
$\endgroup$
  • $\begingroup$ yes - that's the desired end result. $\endgroup$ – Paul_A Feb 16 '15 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.