开发者

Which language understands 'variable a = 0 , 20, ..., 300'?

开发者 https://www.devze.com 2023-01-03 14:57 出处:网络
Which language is smart so that it could understand variable a = 0 , 20, ..., 300 ? so you could easily create arrays with it giving step start var last var (or, better no last variable (a la infinite

Which language is smart so that it could understand variable a = 0 , 20, ..., 300 ? so you could easily create arrays with it giving step start var last var (or, better no last variable (a la infinite array)) and not only for numbers (but even complex numbers and custom structures like Sedenion's which you would probably define on your own as a class or whatever...)

Point is, find a language or algorithm usable in a language that can cach the law of how array of variables you've given (or params of that variables) change. And compose using that law a structure from which you would be able to get any variable(s).

To everyone - examples you provide are very helpful for all beginners out there. And at the same time are the basic knowledge required to build such 'Smart Array' class. So thank you wary much for your enthusiastic help.

As Jeff开发者_StackOverflowSahol noticed

all possible rules might include some that require evaluation of some/all existing members to generate the nth member.

So it is a hard Question. And I think language that would do it 'Naturally' would be great to play\work with, hopefully not only for mathematicians.


Haskell:

Prelude> let a=[0,20..300]
Prelude> a
[0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300]

btw: infinite lists are possible, too:

Prelude> let a=[0,20..]
Prelude> take 20 a
[0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380]


Excel:

  • Write 0 in A1
  • Write 20 in A2
  • Select A1:2
  • Drag the corner downwards


MatLab:

a = [0:20:300]


F#:

> let a = [|0..20..300|];;

val a : int [] =
  [|0; 20; 40; 60; 80; 100; 120; 140; 160; 180; 200; 220; 240; 260; 280; 300|]

With complex numbers:

let c1   = Complex.Create( 0.0, 0.0)
let c2   = Complex.Create(10.0, 10.0)
let a    = [|c1..c2|]

val a : Complex [] =
 [|0r+0i; 1r+0i; 2r+0i; 3r+0i; 4r+0i; 5r+0i; 6r+0i; 7r+0i; 8r+0i; 9r+0i; 10r+0i|]

As you can see it increments only the real part.

If the step is a complex number too, it will increment the real part AND the imaginary part, till the last var real part has been reached:

let step = Complex.Create(2.0, 1.0)
let a    = [|c1..step..c2|]

val a: Complex [] =
  [|0r+0i; 2r+1i; 4r+2i; 6r+3i; 8r+4i; 10r+5i|]

Note that if this behavior doesn't match your needs you still can overload (..) and (.. ..) operators. E.g. you want that it increments the imaginary part instead of the real part:

let (..) (c1:Complex) (c2:Complex) =
  seq {
    for i in 0..int(c2.i-c1.i) do
      yield Complex.Create(c1.r, c1.i + float i)
  }

let a    = [|c1..c2|]
val a : Complex [] =
 [|0r+0i; 0r+1i; 0r+2i; 0r+3i; 0r+4i; 0r+5i; 0r+6i; 0r+7i; 0r+8i; 0r+9i; 0r+10i|]


And PHP:

$a = range(1,300,20);


Wait...

Python:

print range(0, 320, 20)

gives

[0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300]

Props to the comments (I knew there was a more succinct way :P)


Scala:

scala> val a = 0 to 100 by 20
a: scala.collection.immutable.Range = Range(0, 20, 40, 60, 80, 100)

scala> a foreach println
0
20
40
60
80
100

Infinite Lists:

scala> val b = Stream from 1     
b: scala.collection.immutable.Stream[Int] = Stream(1, ?)

scala> b take 5 foreach println
1
2
3
4
5


In python you have

a = xrange(start, stop, step)

(or simply range in python 3) This gives you an iterator from start to stop. It can be infinite since it is built lazily.

>>> a = xrange(0, 300, 20)
>>> for item in a: print item
...
0
20
40
60
80
100
120
140
160
180
200
220
240
260
280


And C++ too [use FC++ library]:

// List is different from STL list
List<int> integers = enumFrom(1); // Lazy list of all numbers starting from 1

// filter and ptr_to_fun definitions provided by FC++
// The idea is to _filter_ prime numbers in this case
// prime is user provided routine that checks if a number is prime
// So the end result is a list of infinite primes :)
List<int> filtered_nums = filter( ptr_to_fun(&prime), integers );  

FC++ lazy list implementation: http://www.cc.gatech.edu/~yannis/fc++/New/new_list_implementation.html

More details: http://www.cc.gatech.edu/~yannis/fc++/

Arpan


Groovy,

assert [ 1, *3..5, 7, *9..<12 ] == [1,3,4,5,7,9,10,11]


The SWYM language, which appears to no longer be online, could infer arithmetic and geometric progressions from a few example items and generate an appropriate list.


I believe the syntax in perl6 is start ... *+increment_value, end


You should instead use math.

- (int) infiniteList: (int)x 
{
    return (x*20);
}

The "smart" arrays use this format since I seriously doubt Haskel could let you do this:

a[1] = 15

after defining a.


C# for example does implement Enumerable.Range(int start, int count), PHP offers the function range(mixed low, mixed high, number step), ... There are programming languages that are "smart" enough.

Beside that, an infinite array is pretty much useless - it's not infinite at all but all-memory-consuming.

You cannot do this enumerating simply with complex numbers as there is no direct successor or predecessor for a given number. Edit: This does not mean that you cannot compare complex numbers or create an array with a specified step!


I may be misunderstanding the question, but the answers that specify way to code the specific example you gave (counting by 20's) don't really meet the requirement that the array "cache" an arbitrary rule for generating array members...it seems that almost any complete solution would require a custom collection class that allows generation of the members with a delegated function/method, especially since all possible rules might include some that require evaluation of some/all existing members to generate the nth member.


Just about any program language can give you this sequence. The question is what syntax you want to use to express it. For example, in C# you can write:

Enumerable.Range(0, 300).Where(x => (x % 20) == 0)

or

for (int i = 0; i < 300; i += 20) yield return i;

or encapsulated in a class:

new ArithmaticSequence(0, 301, 20);

or in a method in a static class:

Enumerable2.ArithmaticSequence(0, 301, 20);

So, what is your criteria?


Assembly: Assuming edi contains the address of the desired array:

xor eax, eax
loop_location:
    mov [edi], eax
    add edi, #4
    add eax, #20
    cmp eax, #300
    jl loop_location


MATLAB it is not a Programming language itself but its a tool but still u can use it like a programming language.

It is built for such Mathematics operations to easily arrays are a breeze there :)

a = 0:1:20;

creates an array from 0 to 20 with an increment of 1. instead of the number 1 you can also provide any value/operation for the increment


Php always does things much simpler, and sometimes dangerously simple too :)


Well… Java is the only language I've ever seriously used that couldn't do that (although I believe using a Vector instead of an Array allowed that).

0

精彩评论

暂无评论...
验证码 换一张
取 消