Skip to main content
added 99 characters in body
Source Link
rm -rf
  • 89.8k
  • 21
  • 303
  • 498

Subsets takes an optional 3rd argument as Subsets[list, {n}, k] that gives you the kth sublist of length n. Since your sublists are in sequence, you'll always need k = 1. You can then use this as:

MapIndexed[First@Subsets[list, #2, 1] &, list] (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

Another alternative would be:

Reverse@Most@NestWhileList[Most, list, # != {} &] 

Subsets takes an optional 3rd argument as Subsets[list, {n}, k] that gives you the kth sublist of length n. Since your sublists are in sequence, you'll always need k = 1. You can then use this as:

MapIndexed[First@Subsets[list, #2, 1] &, list] (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

Subsets takes an optional 3rd argument as Subsets[list, {n}, k] that gives you the kth sublist of length n. Since your sublists are in sequence, you'll always need k = 1. You can then use this as:

MapIndexed[First@Subsets[list, #2, 1] &, list] (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

Another alternative would be:

Reverse@Most@NestWhileList[Most, list, # != {} &] 
Source Link
rm -rf
  • 89.8k
  • 21
  • 303
  • 498

Subsets takes an optional 3rd argument as Subsets[list, {n}, k] that gives you the kth sublist of length n. Since your sublists are in sequence, you'll always need k = 1. You can then use this as:

MapIndexed[First@Subsets[list, #2, 1] &, list] (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *)