Numbers from 1 to 10 (was Numbers from 1 to 10 in Over 4500 Languages)
Author Message
Numbers from 1 to 10 (was Numbers from 1 to 10 in Over 4500 Languages)

Hi Everyone,

An amusing idea so I'll start the ball rolling:

1.  Forth

: ForthNumbers   ( --)   11 1 Do I  2 .R Cr Loop ;

Explanation

The colon starts the definition. Forth uses a data stack to hold
intermediate values. By convention what is on the stack at the beginning
of the procedure and at the end are shown inside the brackets. Here it
starts and ends empty. The Do ... Loop runs round ten times and

I   3 .R

sequence formats and prints out the loop parameters. A carriage return
is performed each time.

2. Prolog

numbers :- number(10, NextNumber).

number(Value, _) :-  Value = 1,  write(Value).

number(Value, NextValue) :-
NextValue is Value - 1,
number(NextValue, _),
nl,
write(Value).

Explanation

Logo programmers will recognise the recursion. The program is called by
numbers. The initial value of 10 is given to number which does all the
work. First is the stop rule. In Prolog variables are destructive and so
passing parameters is done by re-assigning a value and filling the
argument's slot by the new value. This is what happens in the line:

NextValue is Value - 1,

The underscores indicate that we do not care about the value of this
parameter. The :- can be read as saying If.

For a given Value, If the Value = 1 write the value and finish.

I look forward to some interesting languages and variations.

Graham Telfer

Sat, 31 Jul 2004 10:36:06 GMT
Numbers from 1 to 10 (was Numbers from 1 to 10 in Over 4500 Languages)
As far as numbers for human languages, I think the numbers
between 10 and 20 are the most interesting.  In English we
have a unique names for 11 (eleven) and 12 (twelve), but 13
is really constructed from 3+10 (thir{*filter*} = three+ten).

Some languages (Irish i think) have unique name up to 15,
some languages have unique names for all the {*filter*}s,
and some languages have constructed names started at 11.

---------------

Python

Quote:
>>> range(1,11)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Quote:
>>> range(10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Python has a range function to create an iteration list,
but it is zero based, range(10) gives ten numbers, 0 to 9.

Lisp

LISP(1): (do ((i 1 (+ i 1)))
((equal i 11))
(print i))
;;; prints 1 thru 10 on separate lines

LISP(2): (dotimes (i 11) (print i))
;;; prints 0 thru 10 on separate lines

LISP(3): (loop
:for i :from 1 :to 10
:collect i)
(1 2 3 4 5 6 7 8 9 10)  ;;; returns a list from 1 to 10

Lisp also uses zero based expressions and has many ways
to do the same thing.  DO takes a variable name, a start
condition, and iteration expression, a stop condition,
and an expression to evaluate.  DOTIMES starts at 0.
LOOP is a powerful, easy to use macro.

--------------

Thanks,
Jeff Sandys

Quote:

> Hi Everyone,

> An amusing idea so I'll start the ball rolling:

> 1.  Forth

> : ForthNumbers   ( --)   11 1 Do I  2 .R Cr Loop ;

Sun, 01 Aug 2004 02:14:13 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages