Tcl Source Code

Hex Artifact Content
Login

Artifact 734802e31c5db2adc84bd66b34abf7e1f284e5bc:

Attachment "range.n" to ticket [1052584fff] added by antirez 2004-11-02 08:00:15.
0000: 27 5c 22 0a 27 5c 22 20 43 6f 70 79 72 69 67 68  '\".'\" Copyrigh
0010: 74 20 28 63 29 20 32 30 30 34 20 53 61 6c 76 61  t (c) 2004 Salva
0020: 74 6f 72 65 20 53 61 6e 66 69 6c 69 70 70 6f 2e  tore Sanfilippo.
0030: 20 20 41 6c 6c 20 72 69 67 68 74 73 20 72 65 73    All rights res
0040: 65 72 76 65 64 2e 0a 27 5c 22 0a 27 5c 22 20 53  erved..'\".'\" S
0050: 65 65 20 74 68 65 20 66 69 6c 65 20 22 6c 69 63  ee the file "lic
0060: 65 6e 73 65 2e 74 65 72 6d 73 22 20 66 6f 72 20  ense.terms" for 
0070: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 75  information on u
0080: 73 61 67 65 20 61 6e 64 20 72 65 64 69 73 74 72  sage and redistr
0090: 69 62 75 74 69 6f 6e 0a 27 5c 22 20 6f 66 20 74  ibution.'\" of t
00a0: 68 69 73 20 66 69 6c 65 2c 20 61 6e 64 20 66 6f  his file, and fo
00b0: 72 20 61 20 44 49 53 43 4c 41 49 4d 45 52 20 4f  r a DISCLAIMER O
00c0: 46 20 41 4c 4c 20 57 41 52 52 41 4e 54 49 45 53  F ALL WARRANTIES
00d0: 2e 0a 27 5c 22 20 0a 27 5c 22 20 52 43 53 3a 20  ..'\" .'\" RCS: 
00e0: 40 28 23 29 20 24 49 64 3a 20 24 0a 27 5c 22 20  @(#) $Id: $.'\" 
00f0: 0a 2e 73 6f 20 6d 61 6e 2e 6d 61 63 72 6f 73 0a  ..so man.macros.
0100: 2e 54 48 20 72 61 6e 67 65 20 6e 20 22 22 20 54  .TH range n "" T
0110: 63 6c 20 22 54 63 6c 20 42 75 69 6c 74 2d 49 6e  cl "Tcl Built-In
0120: 20 43 6f 6d 6d 61 6e 64 73 22 0a 2e 42 53 0a 27   Commands"..BS.'
0130: 5c 22 20 4e 6f 74 65 3a 20 20 64 6f 20 6e 6f 74  \" Note:  do not
0140: 20 6d 6f 64 69 66 79 20 74 68 65 20 2e 53 48 20   modify the .SH 
0150: 4e 41 4d 45 20 6c 69 6e 65 20 69 6d 6d 65 64 69  NAME line immedi
0160: 61 74 65 6c 79 20 62 65 6c 6f 77 21 0a 2e 53 48  ately below!..SH
0170: 20 4e 41 4d 45 0a 72 61 6e 67 65 20 5c 2d 20 43   NAME.range \- C
0180: 72 65 61 74 65 20 6c 69 73 74 73 20 63 6f 6e 74  reate lists cont
0190: 61 69 6e 69 6e 67 20 61 72 69 74 68 6d 65 74 69  aining arithmeti
01a0: 63 20 70 72 6f 67 72 65 73 73 69 6f 6e 73 0a 2e  c progressions..
01b0: 53 48 20 53 59 4e 4f 50 53 49 53 0a 5c 66 42 72  SH SYNOPSIS.\fBr
01c0: 61 6e 67 65 20 5c 66 49 73 74 61 72 74 20 65 6e  ange \fIstart en
01d0: 64 20 73 74 65 70 0a 2e 62 72 0a 5c 66 42 72 61  d step..br.\fBra
01e0: 6e 67 65 20 5c 66 49 73 74 61 72 74 20 65 6e 64  nge \fIstart end
01f0: 0a 2e 62 72 0a 5c 66 42 72 61 6e 67 65 20 5c 66  ..br.\fBrange \f
0200: 49 65 6e 64 0a 2e 42 45 0a 0a 2e 53 48 20 44 45  Iend..BE...SH DE
0210: 53 43 52 49 50 54 49 4f 4e 0a 2e 50 50 0a 54 68  SCRIPTION..PP.Th
0220: 69 73 20 63 6f 6d 6d 61 6e 64 20 63 72 65 61 74  is command creat
0230: 65 73 20 61 20 6c 69 73 74 20 63 6f 6e 74 61 69  es a list contai
0240: 6e 69 6e 67 20 61 6e 20 61 72 69 74 68 6d 65 74  ning an arithmet
0250: 69 63 20 70 72 6f 67 72 65 73 73 69 6f 6e 2e 0a  ic progression..
0260: 54 68 65 20 61 72 67 75 6d 65 6e 74 73 20 6d 75  The arguments mu
0270: 73 74 20 62 65 20 69 6e 74 65 67 65 72 73 2e 20  st be integers. 
0280: 57 68 65 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64  When the command
0290: 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 0a   is called with.
02a0: 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 2c  three arguments,
02b0: 20 69 74 20 72 65 74 75 72 6e 73 20 61 20 6c 69   it returns a li
02c0: 73 74 20 6f 66 20 69 6e 74 65 67 65 72 73 20 77  st of integers w
02d0: 68 65 72 65 20 74 68 65 20 5c 66 49 69 5c 66 52  here the \fIi\fR
02e0: 2d 74 68 0a 65 6c 65 6d 65 6e 74 20 6f 66 20 74  -th.element of t
02f0: 68 65 20 6c 69 73 74 20 69 73 20 5c 66 49 73 74  he list is \fIst
0300: 61 72 74 2b 69 2a 73 74 65 70 5c 66 52 2e 20 49  art+i*step\fR. I
0310: 66 20 5c 66 49 73 74 65 70 5c 66 52 20 69 73 20  f \fIstep\fR is 
0320: 70 6f 73 69 74 69 76 65 0a 74 68 65 20 6c 61 73  positive.the las
0330: 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  t element of the
0340: 20 72 65 74 75 72 6e 65 64 20 6c 69 73 74 20 69   returned list i
0350: 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6c  s the largest el
0360: 65 6d 65 6e 74 0a 5c 66 49 73 74 61 72 74 2b 69  ement.\fIstart+i
0370: 2a 73 74 65 70 5c 66 52 20 6c 65 73 73 20 74 68  *step\fR less th
0380: 61 6e 20 5c 66 49 65 6e 64 5c 66 52 2e 20 49 66  an \fIend\fR. If
0390: 20 5c 66 49 73 74 65 70 5c 66 52 20 69 73 20 6e   \fIstep\fR is n
03a0: 65 67 61 74 69 76 65 0a 74 68 65 20 6c 61 73 74  egative.the last
03b0: 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
03c0: 72 65 74 75 72 6e 65 64 20 6c 69 73 74 20 69 73  returned list is
03d0: 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c   the smallest el
03e0: 65 6d 65 6e 74 0a 5c 66 49 73 74 61 72 74 2b 69  ement.\fIstart+i
03f0: 2a 73 74 65 70 5c 66 52 20 67 72 65 61 74 65 72  *step\fR greater
0400: 20 74 68 65 6e 20 5c 66 49 65 6e 64 5c 66 52 2e   then \fIend\fR.
0410: 20 54 68 65 20 63 6f 6d 6d 61 6e 64 20 72 65 74   The command ret
0420: 75 72 6e 73 0a 74 68 65 20 65 6d 70 74 79 20 6c  urns.the empty l
0430: 69 73 74 20 69 66 20 5c 66 49 73 74 61 72 74 5c  ist if \fIstart\
0440: 66 52 20 61 6e 64 20 5c 66 49 65 6e 64 5c 66 52  fR and \fIend\fR
0450: 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 76   have the same v
0460: 61 6c 75 65 2e 0a 41 6e 20 65 72 72 6f 72 20 69  alue..An error i
0470: 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
0480: 65 20 72 65 73 75 6c 74 69 6e 67 20 6c 69 73 74  e resulting list
0490: 20 69 73 20 6f 66 20 69 6e 66 69 6e 69 74 65 20   is of infinite 
04a0: 6c 65 6e 67 74 68 0a 6f 72 20 69 66 20 5c 66 49  length.or if \fI
04b0: 73 74 65 70 5c 66 52 20 69 73 20 7a 65 72 6f 2e  step\fR is zero.
04c0: 0a 0a 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20 63  ..This command c
04d0: 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  an be called wit
04e0: 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2c  h two arguments,
04f0: 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a 76 61   omitting the.va
0500: 6c 75 65 20 6f 66 20 5c 66 49 73 74 65 70 5c 66  lue of \fIstep\f
0510: 52 20 74 68 61 74 20 77 69 6c 6c 20 64 65 66 61  R that will defa
0520: 75 6c 74 20 74 6f 20 5c 66 42 31 5c 66 52 2e 20  ult to \fB1\fR. 
0530: 49 74 27 73 20 61 6c 73 6f 0a 70 6f 73 73 69 62  It's also.possib
0540: 6c 65 20 74 6f 20 63 61 6c 6c 20 5c 66 42 72 61  le to call \fBra
0550: 6e 67 65 5c 66 52 20 77 69 74 68 20 6a 75 73 74  nge\fR with just
0560: 20 6f 6e 65 20 61 72 67 75 6d 65 6e 74 2c 20 6f   one argument, o
0570: 6d 69 74 74 69 6e 67 0a 74 68 65 20 76 61 6c 75  mitting.the valu
0580: 65 20 6f 66 20 5c 66 49 73 74 61 72 74 5c 66 52  e of \fIstart\fR
0590: 20 74 68 61 74 20 77 69 6c 6c 20 64 65 66 61 75   that will defau
05a0: 6c 74 20 74 6f 20 5c 66 42 30 5c 66 52 20 61 6e  lt to \fB0\fR an
05b0: 64 0a 5c 66 49 73 74 65 70 5c 66 52 20 74 68 61  d.\fIstep\fR tha
05c0: 74 20 77 69 6c 6c 20 64 65 66 61 75 6c 74 20 74  t will default t
05d0: 6f 20 5c 66 42 31 5c 66 52 2e 0a 0a 54 68 65 20  o \fB1\fR...The 
05e0: 5c 66 42 72 61 6e 67 65 5c 66 52 20 63 6f 6d 6d  \fBrange\fR comm
05f0: 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  and is implement
0600: 65 64 20 69 6e 20 61 20 77 61 79 20 74 68 61 74  ed in a way that
0610: 20 6d 61 6b 65 73 0a 69 74 20 70 6f 73 73 69 62   makes.it possib
0620: 6c 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  le to store the 
0630: 67 65 6e 65 72 61 74 65 64 20 6c 69 73 74 73 20  generated lists 
0640: 69 6e 20 63 6f 6e 73 74 61 6e 74 20 73 70 61 63  in constant spac
0650: 65 0a 75 6e 64 65 72 20 63 65 72 74 61 69 6e 20  e.under certain 
0660: 63 6f 6e 64 69 74 69 6f 6e 73 2c 20 72 65 67 61  conditions, rega
0670: 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 61 63  rdless of the ac
0680: 74 75 61 6c 20 76 61 6c 75 65 20 6f 66 0a 5c 66  tual value of.\f
0690: 49 73 74 61 72 74 5c 66 52 2c 20 5c 66 49 65 6e  Istart\fR, \fIen
06a0: 64 5c 66 52 2c 20 61 6e 64 20 5c 66 49 73 74 65  d\fR, and \fIste
06b0: 70 5c 66 52 2e 20 41 63 63 65 73 73 69 6e 67 20  p\fR. Accessing 
06c0: 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 6c 69  the resulting.li
06d0: 73 74 20 6f 6e 6c 79 20 75 73 69 6e 67 20 74 68  st only using th
06e0: 65 20 63 6f 6d 6d 61 6e 64 73 20 5c 66 42 66 6f  e commands \fBfo
06f0: 72 65 61 63 68 5c 66 52 2c 20 5c 66 42 6c 69 6e  reach\fR, \fBlin
0700: 64 65 78 5c 66 52 20 61 6e 64 20 5c 66 42 6c 6c  dex\fR and \fBll
0710: 65 6e 67 74 68 5c 66 52 0a 73 68 6f 75 6c 64 20  ength\fR.should 
0720: 61 6c 6c 6f 77 20 74 68 65 20 75 73 65 72 20 74  allow the user t
0730: 6f 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65  o take advantage
0740: 20 6f 66 20 74 68 69 73 20 6f 70 74 69 6d 69 7a   of this optimiz
0750: 61 74 69 6f 6e 2e 0a 54 68 65 20 6f 70 74 69 6d  ation..The optim
0760: 69 7a 61 74 69 6f 6e 20 69 73 20 74 72 61 6e 73  ization is trans
0770: 70 61 72 65 6e 74 20 74 6f 20 74 68 65 20 75 73  parent to the us
0780: 65 72 2c 20 73 6f 20 5c 66 42 72 61 6e 67 65 5c  er, so \fBrange\
0790: 66 52 20 72 65 74 75 72 6e 73 0a 74 72 75 65 20  fR returns.true 
07a0: 54 63 6c 20 6c 69 73 74 73 20 75 6e 64 65 72 20  Tcl lists under 
07b0: 65 76 65 72 79 20 70 72 61 63 74 69 63 61 6c 20  every practical 
07c0: 61 73 70 65 63 74 2e 0a 2e 53 48 20 45 58 41 4d  aspect...SH EXAM
07d0: 50 4c 45 0a 53 6f 6d 65 20 65 78 61 6d 70 6c 65  PLE.Some example
07e0: 20 6f 66 20 6f 75 74 70 75 74 2e 0a 2e 43 53 0a   of output...CS.
07f0: 25 20 72 61 6e 67 65 20 31 30 0a 30 20 31 20 32  % range 10.0 1 2
0800: 20 33 20 34 20 35 20 36 20 37 20 38 20 39 0a 25   3 4 5 6 7 8 9.%
0810: 20 72 61 6e 67 65 20 35 20 33 30 20 35 0a 35 20   range 5 30 5.5 
0820: 31 30 20 31 35 20 32 30 20 32 35 0a 25 20 72 61  10 15 20 25.% ra
0830: 6e 67 65 20 31 30 20 30 20 2d 32 0a 31 30 20 38  nge 10 0 -2.10 8
0840: 20 36 20 34 20 32 0a 25 20 72 61 6e 67 65 20 38   6 4 2.% range 8
0850: 20 2d 32 20 2d 33 0a 38 20 35 20 32 20 2d 31 0a   -2 -3.8 5 2 -1.
0860: 25 20 6c 6c 65 6e 67 74 68 20 5b 72 61 6e 67 65  % llength [range
0870: 20 35 20 35 5d 20 0a 30 0a 2e 43 45 0a 0a 55 73   5 5] .0..CE..Us
0880: 69 6e 67 20 5c 66 42 72 61 6e 67 65 5c 66 52 20  ing \fBrange\fR 
0890: 61 6e 64 20 5c 66 42 66 6f 72 65 61 63 68 5c 66  and \fBforeach\f
08a0: 52 20 61 73 20 61 6c 74 65 72 6e 61 74 69 76 65  R as alternative
08b0: 20 74 6f 20 5c 66 42 66 6f 72 5c 66 52 2e 0a 2e   to \fBfor\fR...
08c0: 43 53 0a 25 20 73 65 74 20 74 6f 74 61 6c 20 30  CS.% set total 0
08d0: 0a 30 0a 25 20 66 6f 72 65 61 63 68 20 78 20 5b  .0.% foreach x [
08e0: 72 61 6e 67 65 20 31 30 5d 20 7b 69 6e 63 72 20  range 10] {incr 
08f0: 74 6f 74 61 6c 20 24 78 7d 0a 25 20 73 65 74 20  total $x}.% set 
0900: 74 6f 74 61 6c 0a 34 35 0a 2e 43 45 0a 0a 55 73  total.45..CE..Us
0910: 69 6e 67 20 5c 66 42 72 61 6e 67 65 5c 66 52 20  ing \fBrange\fR 
0920: 61 6e 64 20 5c 66 42 65 78 70 72 5c 66 52 20 74  and \fBexpr\fR t
0930: 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 66 61  o compute the fa
0940: 63 74 6f 72 69 61 6c 20 66 75 6e 63 74 69 6f 6e  ctorial function
0950: 2e 0a 2e 43 53 0a 25 20 65 78 70 72 20 5b 6a 6f  ...CS.% expr [jo
0960: 69 6e 20 5b 72 61 6e 67 65 20 35 20 31 20 2d 31  in [range 5 1 -1
0970: 5d 20 2a 5d 0a 31 32 30 0a 2e 43 45 0a 0a 55 73  ] *].120..CE..Us
0980: 69 6e 67 20 5c 66 42 72 61 6e 67 65 5c 66 52 20  ing \fBrange\fR 
0990: 77 69 74 68 20 61 6e 20 68 65 6c 70 65 72 20 5c  with an helper \
09a0: 66 42 6d 61 70 5c 66 52 20 66 75 6e 63 74 69 6f  fBmap\fR functio
09b0: 6e 20 74 68 61 74 20 6d 61 70 73 0a 61 20 54 63  n that maps.a Tc
09c0: 6c 20 73 63 72 69 70 74 20 74 6f 20 61 20 6c 69  l script to a li
09d0: 73 74 20 74 6f 20 67 65 6e 65 72 61 74 65 20 74  st to generate t
09e0: 68 65 20 73 71 75 61 72 65 20 6f 66 20 74 68 65  he square of the
09f0: 20 66 69 72 73 74 20 74 65 6e 0a 6e 61 74 75 72   first ten.natur
0a00: 61 6c 20 6e 75 6d 62 65 72 73 2e 0a 2e 43 53 0a  al numbers...CS.
0a10: 25 20 70 72 6f 63 20 6d 61 70 20 7b 76 61 72 6e  % proc map {varn
0a20: 61 6d 65 20 73 63 72 69 70 74 20 6d 79 6c 69 73  ame script mylis
0a30: 74 7d 20 7b 0a 3e 20 20 20 20 75 70 76 61 72 20  t} {.>    upvar 
0a40: 24 76 61 72 6e 61 6d 65 20 76 61 72 0a 3e 20 20  $varname var.>  
0a50: 20 20 73 65 74 20 72 65 73 20 7b 7d 0a 3e 20 20    set res {}.>  
0a60: 20 20 66 6f 72 65 61 63 68 20 76 61 72 20 24 6d    foreach var $m
0a70: 79 6c 69 73 74 20 7b 0a 3e 20 20 20 20 20 20 20  ylist {.>       
0a80: 20 6c 61 70 70 65 6e 64 20 72 65 73 20 5b 75 70   lappend res [up
0a90: 6c 65 76 65 6c 20 31 20 24 73 63 72 69 70 74 5d  level 1 $script]
0aa0: 0a 3e 20 20 20 20 7d 0a 3e 20 20 20 20 72 65 74  .>    }.>    ret
0ab0: 75 72 6e 20 24 72 65 73 0a 3e 7d 0a 25 20 6d 61  urn $res.>}.% ma
0ac0: 70 20 78 20 7b 65 78 70 72 20 7b 24 78 2a 24 78  p x {expr {$x*$x
0ad0: 7d 7d 20 5b 72 61 6e 67 65 20 30 20 31 30 5d 0a  }} [range 0 10].
0ae0: 30 20 31 20 34 20 39 20 31 36 20 32 35 20 33 36  0 1 4 9 16 25 36
0af0: 20 34 39 20 36 34 20 38 31 0a 2e 43 45 0a 0a 2e   49 64 81..CE...
0b00: 53 48 20 22 53 45 45 20 41 4c 53 4f 22 0a 66 6f  SH "SEE ALSO".fo
0b10: 72 28 6e 29 2c 20 6c 69 73 74 28 6e 29 2c 20 6c  r(n), list(n), l
0b20: 69 6e 64 65 78 28 6e 29 2c 20 6c 69 6e 73 65 72  index(n), linser
0b30: 74 28 6e 29 2c 20 6c 6c 65 6e 67 74 68 28 6e 29  t(n), llength(n)
0b40: 2c 20 0a 2e 56 53 20 38 2e 34 0a 6c 73 65 74 28  , ..VS 8.4.lset(
0b50: 6e 29 0a 2e 56 45 0a 6c 73 6f 72 74 28 6e 29 2c  n)..VE.lsort(n),
0b60: 20 6c 72 61 6e 67 65 28 6e 29 0a 0a 2e 53 48 20   lrange(n)...SH 
0b70: 4b 45 59 57 4f 52 44 53 0a 61 72 69 74 68 6d 65  KEYWORDS.arithme
0b80: 74 69 63 20 70 72 6f 67 72 65 73 73 69 6f 6e 2c  tic progression,
0b90: 20 72 61 6e 67 65 2c 20 69 74 65 72 61 74 69 6f   range, iteratio
0ba0: 6e 2c 20 6c 6f 6f 70 69 6e 67 0a                 n, looping.