views:

1129

answers:

20

What 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 what ever...)

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 every one - 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 JeffSahol 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 'Naturaly' would be great to play\work with, hopfully not only for mathematitions...

+33  A: 

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]
Johannes Weiß
Since I don't know Haskell, can you change `a` after defining it? Ex. `a[1] = 15` and leave all the other values unchanged so that the next `take 20 a` returns `[0, 15, 40, 60, 80, ..]` ?
Stephen Furlani
how about complex numbers and costume structures?
Blender
@Ole Jack: What is a "costume structure"? Also, I'm curious: why do you want to do this with non-integers? I'm not sure this would even make sense for complex numbers, and I have no idea what you mean by "costume structure".
FrustratedWithFormsDesigner
http://en.wikipedia.org/wiki/Octonion Octonion's can be defined as class or something like that...
Blender
@Ole Jack: Ok, octonion's look interesting, but I'm still not sure you how you got from "costume structure" to "octonion". True, I'm not that familiar with this branch of mathematics, but a google search (I was just curious about the terminology) for "octonion costume structure" returns... *this* page as the first hit. ;)
FrustratedWithFormsDesigner
@Stephan: You cannot rebind variables or mutate data structures in Haskell.
FredOverflow
Maybe Ole means custom structures?
starblue
I like this notation. Looking through the other answers it is the only one that is close enough to mathematical notation to be self-explanatory.
starblue
A: 

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).

David Dorward
The c family (arpan's cleverness above not withstanding), pascal, /bin/sh derivatives, /bin/csh derivatives, fortran at least through the '80s, ...
dmckee
I'm not saying they don't exist (you haven't listen any that I've used for anything serious), just that ones which support sparse arrays are *very* common.
David Dorward
Perl can't do that with or, it would be weird anyway with the core constructs `perl -E'say for grep $_%20==0, 0..300'` You could roll your own.
Evan Carroll
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!

Marius Schulz
by infinit I mant that you can get any item when you want like a[12309987657] when you need it.
Blender
infinite lists are NOT useless at all! Sure, the programming language should be lazy. If it's not, it does definetly makes no sense *g*. (btw.: Haskell is lazy)
Johannes Weiß
`Enumerable.Range` is not a *language* feature, but rather a *runtime library* feature. I'll let the reader decide if that is significant.
Marc Gravell
+1 for the complex numbers comment.
FrustratedWithFormsDesigner
There is no successor for a complex number, but when a step size is provided then there exists a successor for any element of a complex range.
Ben Voigt
Since the rational number domain is countable, it is pretty trivial to see that complex numbers are countable (and therefore enumerable) as well. It just becomes a challenge to order them. Perhaps using http://upload.wikimedia.org/wikipedia/commons/8/85/Diagonal_argument.svg?
DonaldRay
@Johannes Weiß: Lazy behaviour is perfectly okay, I just misunderstood "infinite array".@Donald Ray: See Ben Voigts comment on the step. If there is one (or two, respectively) provided, the complex number generation is fine.
Marius Schulz
@Donald: The complex domain isn't defined in terms of rationals but in terms of reals. So it isn't countable nor enumerable. My point was that an arithmetic sequence is ordered even when the space it lies in is not.
Ben Voigt
@Ben: Ahh, I thought we were sticking with... err, well I guess you could say natural complex numbers, where both a and b were integers.
DonaldRay
+1  A: 

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
Francesco
OP asked for complex numbers as well. AFAIK, xrange only does integers.
Timothy
@Timothy: would it make sense to do this for complex numbers?
FrustratedWithFormsDesigner
Whether or not it makes sense depends on the particular problem at hand. I'm just noting the OP specifically asked complex numbers as well.
Timothy
+4  A: 

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)

Dominic Bou-Samra
`[y for y in x]` is different than just `x` ? Needless list comprehension.
Evan Carroll
@Evan Carroll: It is - of course - when `x` is a *generator expression* rather than a list. The list comprehension forces evaluation of the expression into a list that would otherwise just be evaluated on demand. Note that this is equivalent to `list(x)`.
Dario
would print() evaluate the generator expression? because for me `print range(0,320,20)` is the same as `print [y for y in range(0, 320, 20)]`
Evan Carroll
+11  A: 

MatLab:

a = [0:20:300]
Ben Voigt
and for complex, dual qua... etc?
Blender
@Ole Jack: Could you please explain a "dual qua"?
FrustratedWithFormsDesigner
Complex numbers are fully supported in MatLab. I don't have it in front of me right now to test, but just as the arithmetic sequence above can be written as: `a = [0:15] * 20`, an arithmetic sequence of complex numbers can be produced easily, e.g. `a = (1 + 3i) + [1:100] * (1 + 2i)`
Ben Voigt
+2  A: 

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.

Stephen Furlani
is it what C or C++ ?
Blender
Objective-C but it doesn't matter any language will do.
Stephen Furlani
That's objective-c but the principle holds for any of the C language family and actually his example leaves out a lot of the boiler plate required.
Jeremy Wall
Well, yes, Ideally you could create an entire "smart array" Class using my example.
Stephen Furlani
Haskell doesn't allow yout to do `a[1] = 15`, that is true (but for different reasons than you're implying). However your implication seems to be that lists of this form are implemented as functions in haskell and not stored in memory. That is false. The list `[1,3..7]` is (lazily) stored in memory the same as the list `[1,3,5,7]`. As a matter of fact writing `[1,3..7]` is equivalent to writing `[1,3,5,7]` in every possible way.
sepp2k
Not knowing Haskell, using math for [1,3..90] would be much more memory efficient than storing even a lazy array. Since Haskell won't let you change it after it is defined, then there's no reason to store a 100-value immutable array in memory if a simple math equation can represent the same thing.
Stephen Furlani
@Stephen: It's a (linked) list, not an array, and yes, there is a point: By storing it as a list, all list operations work on it by default. If you'd implement it as a custom type wrapping a function, you'd have to redefine all list operations for that type. Even more importantly: you can use the list as the tail of another list. I.e. you can do `myList = 42 : [1..10]`, you couldn't do that if `[1..10]` weren't a list. You also couldn't use pattern matching with a type build around a function. Also operations like map, would need to have a non-standard type to work with such a type.
sepp2k
@sepp2k: `myList = 42 : mySmartObject.array` could arguably do the same thing. Granted, if you wanted smart infinite lists and arrays there's no reason you couldn't write a custom class to handle all that operator overloading. Linked Lists in most languages are classes anyway - (C++ STL, Java come to mind)
Stephen Furlani
@Stephen: Yes, it could work by adding a call to something like `toList`, but that would make the code more verbose. You could also change the type of `:` so that the second operator could be any list-like collection, not necessarily a plain list, but that would complicate its type signature quite a bit (also it's not trivial to come up with a sensible signature for `:` here). The fact remains that `[1..10]` in haskell is a list. It could have been implemented another way, but it wasn't. (Also as a side-note List (well `[]`) in haskell is an algebraic data type - haskell has no (OO-)classes).
sepp2k
lol, seeing as how I code in ObjC verbosity is kindof a way of life.
Stephen Furlani
+4  A: 

And PHP:

$a = range(1,300,20);
msakr
PHP looking good? Never thought I'd see the day.
Dominic Bou-Samra
can you put random or function instead of 20?
Blender
@Ole Jak, Yes, you can put any number there.
strager
+9  A: 

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|]
Stringer Bell
Example for complex numbers and costume structures would be grate!..
Blender
I've edited my answer. Note that you can define those operators so that they work on your *costume structures* too.
Stringer Bell
+18  A: 

Excel:

  • Write 0 in A1
  • Write 20 in A2
  • Select A1:2
  • Drag the corner downwards
shoosh
fany one! grate!
Blender
lol but it's not infinite. You'll run out of rows at some point.
Stephen Furlani
Is this really a programming solution? I see it more as a static spreadsheet solution, but YMMV.
Kimball Robinson
select whole column and from menu Edit->Fill->Down
Imre L
A: 

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

Wind Chimez
Grate! Sow how!
Blender
+1  A: 

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

Fanatic23
+1  A: 

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.

JeffSahol
So... what can I say - examples thay provided can be at least helpfull for all beginners out there... And at the same time are the bacik knoledge required to built such 'Smart Array' class... And Yes - all possible rules might include some that require evaluation of some/all existing members to generate the nth member. So it is hard Question. And language that would do it 'Naturaly' would be grate to play\work with... possibly
Blender
+2  A: 

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
missingfaktor
+1  A: 

Groovy,

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

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?

Jay Bazuzi
A: 

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

KiNGPiN
What's the difference between a programming language and something the you use exactly like a programing language but for some unspecified reason is not a programming language by your account?
shoosh
Matlab isn't a programming language, it's an implementation. octave is a free implementation of the same language. But it's a lot more convenient to talk about Matlab instead of "the programming language used by Matlab". (Similarly one might prefer to succinctly say "the GCC language" instead of "the C99 language plus GCC-specific extensions" even though the latter is more correct).
Ben Voigt
A: 

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
dig
+1  A: 

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.

munificent
+1  A: 

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

Daenyth