11

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 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 'Naturally' would be great to play\work with, hopefully not only for mathematicians.

Community
  • 1
  • 1
Rella
  • 65,003
  • 109
  • 363
  • 636
  • by infinit I mant that you can get any item when you want like a[12309987657] when you need it. – Rella Jun 14 '10 at 13:31
  • 5
    That's not necessarily a sign of a smart language, but of a language which has that as a built-in or part of the core libraries. It's easy to add this in a library for any language, simply by parsing the string. – Robert Munteanu Jun 14 '10 at 13:37
  • 4
    It seems that most of the answers (except the Haskell one) have completely misunderstood the question. The interesting part is to not have to *give* the step size explicitly but have the language compute it automatically from a pattern. You know, the 'smart' part of the question?!? – Jacques Carette Jun 14 '10 at 13:45
  • @ Jacques Carette +1 absolutly – Rella Jun 14 '10 at 13:47
  • @Jacques Carette, The Haskell does imply the step size. I strongly recommend using MatLab or Mathematica. No language natively supports complex numbers - and, without the STL, C++ doesn't even support vectors. – Stephen Furlani Jun 14 '10 at 14:28
  • 2
    @Stephen Furlani @'No language natively supports complex number ' see _Imaginary in C, C99 standard – Rella Jun 14 '10 at 14:38
  • 2
    @Ole `` is part of the standard library, not the language. Rather large difference there. http://en.wikipedia.org/wiki/Complex.h – Stephen Furlani Jun 14 '10 at 14:56
  • 4
    @Ole Jak No language natively supports complex numbers? Ever heard of FORTRAN? No, _you_ probably haven't,/ –  Jun 14 '10 at 15:47
  • 2
    Python has always provided native support for complex numbers. – Don O'Donnell Jun 15 '10 at 06:17

20 Answers20

35

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 Weiss
  • 52,533
  • 16
  • 102
  • 136
  • 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 Jun 14 '10 at 13:36
  • how about complex numbers and costume structures? – Rella Jun 14 '10 at 13:36
  • @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 Jun 14 '10 at 13:41
  • http://en.wikipedia.org/wiki/Octonion Octonion's can be defined as class or something like that... – Rella Jun 14 '10 at 14:02
  • 1
    @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 Jun 14 '10 at 15:30
  • 6
    @Stephan: You cannot rebind variables or mutate data structures in Haskell. – fredoverflow Jun 14 '10 at 16:39
  • 2
    Maybe Ole means custom structures? – starblue Jun 14 '10 at 20:09
  • 1
    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 Jun 14 '10 at 20:13
20

Excel:

  • Write 0 in A1
  • Write 20 in A2
  • Select A1:2
  • Drag the corner downwards
shoosh
  • 76,898
  • 55
  • 205
  • 325
12

MatLab:

a = [0:20:300]
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • and for complex, dual qua... etc? – Rella Jun 14 '10 at 13:45
  • 1
    @Ole Jack: Could you please explain a "dual qua"? – FrustratedWithFormsDesigner Jun 14 '10 at 17:08
  • 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 Jun 15 '10 at 00:28
10

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|]
elmattic
  • 12,046
  • 5
  • 43
  • 79
  • 1
    I've edited my answer. Note that you can define those operators so that they work on your *costume structures* too. – elmattic Jun 14 '10 at 15:20
5

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
  • 14,799
  • 26
  • 100
  • 156
  • `[y for y in x]` is different than just `x` ? Needless list comprehension. – Evan Carroll Jun 14 '10 at 17:08
  • @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 Jun 14 '10 at 19:05
  • 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 Jun 14 '10 at 19:15
5

And PHP:

$a = range(1,300,20);
kamasheto
  • 1,020
  • 6
  • 12
3

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
Community
  • 1
  • 1
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
2

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
  • 3,200
  • 1
  • 34
  • 46
2

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
  • 3,378
  • 2
  • 28
  • 51
2

Groovy,

assert [ 1, *3..5, 7, *9..<12 ] == [1,3,4,5,7,9,10,11]
James Anderson
  • 27,109
  • 7
  • 50
  • 78
2

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
  • 11,946
  • 2
  • 38
  • 55
2

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

Daenyth
  • 35,856
  • 13
  • 85
  • 124
1

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
  • 15,976
  • 12
  • 63
  • 97
  • by infinit I mant that you can get any item when you want like a[12309987657] when you need it. – Rella Jun 14 '10 at 13:32
  • 9
    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 Weiss Jun 14 '10 at 13:32
  • 1
    `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 Jun 14 '10 at 13:35
  • 1
    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 Jun 14 '10 at 13:45
  • 1
    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 Jun 14 '10 at 13:49
  • @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 Jun 14 '10 at 13:54
  • @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 Jun 15 '10 at 22:43
  • @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 Jun 17 '10 at 12:27
1

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
  • 6,794
  • 4
  • 31
  • 60
  • 1
    Objective-C but it doesn't matter any language will do. – Stephen Furlani Jun 14 '10 at 13:59
  • 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 Jun 14 '10 at 14:02
  • Well, yes, Ideally you could create an entire "smart array" Class using my example. – Stephen Furlani Jun 14 '10 at 14:04
  • 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 Aug 29 '10 at 12:32
  • 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 Aug 30 '10 at 18:15
  • @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 Sep 03 '10 at 13:03
  • @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 Sep 03 '10 at 13:58
  • @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 Sep 03 '10 at 14:18
  • lol, seeing as how I code in ObjC verbosity is kindof a way of life. – Stephen Furlani Sep 07 '10 at 12:23
1

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
  • 971
  • 8
  • 19
  • 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 – Rella Jun 14 '10 at 14:53
1

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
  • 45,157
  • 15
  • 111
  • 168
1

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
  • 3,850
  • 3
  • 27
  • 33
0

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

Basit Anwer
  • 6,742
  • 7
  • 45
  • 88
  • 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 Jun 15 '10 at 06:28
  • 2
    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 Jun 15 '10 at 22:47
-1

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

Wind Chimez
  • 1,166
  • 1
  • 9
  • 19
-8

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • The c family (arpan's cleverness above not withstanding), pascal, /bin/sh derivatives, /bin/csh derivatives, fortran at least through the '80s, ... – dmckee --- ex-moderator kitten Jun 14 '10 at 15:12
  • 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. – Quentin Jun 14 '10 at 15:27
  • 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 Jun 14 '10 at 19:19