task_url
stringlengths
30
116
task_name
stringlengths
2
86
task_description
stringlengths
0
14.4k
language_url
stringlengths
2
53
language_name
stringlengths
1
52
code
stringlengths
0
61.9k
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#PARI.2FGP
PARI/GP
zz(n)={ my(M=matrix(n,n),i,j,d=-1,start,end=n^2-1); while(ct--, M[i+1,j+1]=start; M[n-i,n-j]=end; start++; end--; i+=d; j-=d; if(i<0, i++; d=-d , if(j<0, j++; d=-d ) ); if(start>end,return(M)) ) };
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Fortran
Fortran
program doors implicit none integer, allocatable :: door(:) character(6), parameter :: s(0:1) = [character(6) :: "closed", "open"] integer :: i, n   print "(A)", "Number of doors?" read *, n allocate (door(n)) door = 1 do i = 1, n door(i:n:i) = 1 - door(i:n:i) print "...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#MIPS_Assembly
MIPS Assembly
  .data array: .word 1, 2, 3, 4, 5, 6, 7, 8, 9 # creates an array of 9 32 Bit words.   .text main: la $s0, array li $s1, 25 sw $s1, 4($s0) # writes $s1 (25) in the second array element # the four counts thi bytes after the beginning of the address. 1 word = 4 bytes, so 4 acesses the second element   lw $s2, 20(...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Phix
Phix
with javascript_semantics function call_fn(integer f, n) return call_func(f,{f,n}) end function function Y(integer f) return f end function function fac(integer self, integer n) return iff(n>1?n*call_fn(self,n-1):1) end function function fib(integer self, integer n) return iff(n>1?call_fn(self,n-1...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Pascal
Pascal
Program zigzag( input, output );   const size = 5; var zzarray: array [1..size, 1..size] of integer; element, i, j: integer; direction: integer; width, n: integer;   begin i := 1; j := 1; direction := 1; for element := 0 to (size*size) - 1 do begin zzarray[i,j] := element; i := i + direction...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Free_Pascal
Free Pascal
  program OneHundredIsOpen;   const DoorCount = 100;   var IsOpen: array[1..DoorCount] of boolean; Door, Jump: integer;   begin // Close all doors for Door := 1 to DoorCount do IsOpen[Door] := False; // Iterations for Jump := 1 to DoorCount do begin Door := Jump; repeat IsOpen[Door] :=...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Modula-2
Modula-2
VAR staticArray: ARRAY [1..10] OF INTEGER;
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Phixmonti
Phixmonti
0 var subr   def fac dup 1 > if dup 1 - subr exec * endif enddef   def fib dup 1 > if dup 1 - subr exec swap 2 - subr exec + endif enddef   def test print ": " print var subr 10 for subr exec print " " print endfor nl enddef   getid fac "fac" test getid fib "f...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#PHP
PHP
<?php function Y($f) { $g = function($w) use($f) { return $f(function() use($w) { return call_user_func_array($w($w), func_get_args()); }); }; return $g($g); }   $fibonacci = Y(function($f) { return function($i) use($f) { return ($i <= 1) ? $i : ($f($i-1) + $f($i-2)); }; });   echo $fibonacci(10),...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Perl
Perl
use 5.010;   sub zig_zag { my $n = shift; my $max_number = $n**2; my @matrix; my $number = 0; for my $j ( 0 .. --$n ) { for my $i ( $j % 2 ? 0 .. $j : reverse 0 .. $j ) { $matrix[$i][ $j - $i ] = $number++; ...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#FreeBASIC
FreeBASIC
' version 27-10-2016 ' compile with: fbc -s console   #Define max_doors 100   Dim As ULong c, n, n1, door(1 To max_doors)   ' toggle, at start all doors are closed (0) ' 0 = door closed, 1 = door open For n = 1 To max_doors For n1 = n To max_doors Step n door(n1) = 1 - door(n1) Next Next   ' count the d...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Modula-3
Modula-3
VAR staticArray: ARRAY [1..10] OF INTEGER;
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#PicoLisp
PicoLisp
(de Y (F) (let X (curry (F) (Y) (F (curry (Y) @ (pass (Y Y))))) (X X) ) )
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Pop11
Pop11
define Y(f); procedure (x); x(x) endprocedure( procedure (y); f(procedure(z); (y(y))(z) endprocedure) endprocedure ) enddefine;   define fac(h); procedure (n); if n = 0 then 1 else n * h(n - 1) endif endprocedure enddefine;   define fib(h); procedure (n); i...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Phix
Phix
with javascript_semantics integer n = 9 integer zstart = 0, zend = n*n-1 --integer zstart = 1, zend = n*n string fmt = sprintf("%%%dd",length(sprintf("%d",zend))) sequence m = repeat(repeat("??",n),n) integer x = 1, y = 1, d = -1 while 1 do m[x][y] = sprintf(fmt,zstart) if zstart=zend then exit end if zstar...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#friendly_interactive_shell
friendly interactive shell
# Set doors to empty list set doors   # Initialize doors arrays for i in (seq 100) set doors[$i] 0 end   for i in (seq 100) set j $i while test $j -le 100 # Logical not on doors set doors[$j] (math !$doors[$j]) set j (math $j + $i) end end   # Print every door for i in (seq (coun...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Monte
Monte
var myArray := ['a', 'b', 'c','d']
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#PostScript
PostScript
y { {dup cons} exch concat dup cons i }.   /fac { { {pop zero?} {pop succ} {{dup pred} dip i *} ifte } y }.
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#PowerShell
PowerShell
$fac = { param([ScriptBlock] $f) invoke-expression @" { param([int] `$n) if (`$n -le 0) {1} else {`$n * {$f}.InvokeReturnAsIs(`$n - 1)} } "@ }   $fib = { param([ScriptBlock] $f) invoke-expression @" { param([int] `$n) switch (`$n) { 0 {1} 1 {1} ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Phixmonti
Phixmonti
5 var Size 0 Size repeat Size repeat   1 var i 1 var j   Size 2 power for swap i get rot j set i set i j + 1 bitand 0 == IF j Size < IF j 1 + var j ELSE i 2 + var i ENDIF i 1 > IF i 1 - var i ENDIF ELSE i Size < IF i 1 + var i ELSE j 2 + var j ENDIF j 1 > IF j 1 - var j ENDI...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Frink
Frink
  doors = new array[[101], false] for pass=1 to 100 for door=pass to 100 step pass doors@door = ! doors@door   print["Open doors: "] for door=1 to 100 if doors@door print["$door "]  
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Nanoquery
Nanoquery
// create a fixed-length array (length 10) arr = array(10)   // assign a value to the first position in the array and then display it arr[0] = "hello, world!" println arr[0]     // create a variable-length list l = list()   // place the numbers 1-10 in the list for i in range(1,10) append l i end   // display the list...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Prolog
Prolog
:- use_module(lambda).   % The Y combinator y(P, Arg, R) :- Pred = P +\Nb2^F2^call(P,Nb2,F2,P), call(Pred, Arg, R).     test_y_combinator :- % code for Fibonacci function Fib = \NFib^RFib^RFibr1^(NFib < 2 -> RFib = NFib ; NFib1 is NFib - 1, NFib2 is NFib - 2, ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#PHP
PHP
function ZigZagMatrix($num) { $matrix = array(); for ($i = 0; $i < $num; $i++){ $matrix[$i] = array(); }   $i=1; $j=1; for ($e = 0; $e < $num*$num; $e++) { $matrix[$i-1][$j-1] = $e; if (($i + $j) % 2 == 0) { if ($j < $num){ $j++; }else{ $i += 2; } ...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#FunL
FunL
for i <- 1..100 r = foldl1( \a, b -> a xor b, [(a|i) | a <- 1..100] ) println( i + ' ' + (if r then 'open' else 'closed') )
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Neko
Neko
var myArray = $array(1);   $print(myArray[0]);
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Python
Python
>>> Y = lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args))) >>> fac = lambda f: lambda n: (1 if n<2 else n*f(n-1)) >>> [ Y(fac)(i) for i in range(10) ] [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] >>> fib = lambda f: lambda n: 0 if n == 0 else (1 if n == 1 else f(n-1) + f(n-2)) >>> [ Y(fib)(i) for i i...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#PicoLisp
PicoLisp
(load "@lib/simul.l")   (de zigzag (N) (prog1 (grid N N) (let (D '(north west south east .) E '(north east .) This 'a1) (for Val (* N N) (=: val Val) (setq This (or ((cadr D) ((car D) This)) (prog (setq...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#PL.2FI
PL/I
/* Fill a square matrix with the values 0 to N**2-1, */ /* in a zig-zag fashion. */ /* N is the length of one side of the square. */ /* Written 22 February 2010. */   declare n fixed binary;   put skip list ('Please type the size of the ma...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Futhark
Futhark
  let main(n: i32): [n]bool = loop is_open = replicate n false for i < n do let js = map (*i+1) (iota n) let flips = map (\j -> if j < n then unsafe !is_open[j] else true -- Doesn't matter. ) js in scatter is_open js ...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Nemerle
Nemerle
using System; using System.Console; using System.Collections;   module ArrayOps { Main() : void { def fives = array(10); foreach (i in [1 .. 10]) fives[i - 1] = i * 5; def ten = fives[1]; WriteLine($"Ten: $ten");   def dynamic = ArrayList(); dynamic.Add(1); ...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Q
Q
> Y: {{x x} {({y {(x x) y} x} y) x} x} > fac: {{$[y<2; 1; y*x y-1]} x} > (Y fac) 6 720j > fib: {{$[y<2; 1; (x y-1) + (x y-2)]} x} > (Y fib) each til 20 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765  
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Plain_TeX
Plain TeX
\long\def\antefi#1#2\fi{#2\fi#1} \def\fornum#1=#2to#3(#4){% \edef#1{\number\numexpr#2}\edef\fornumtemp{\noexpand\fornumi\expandafter\noexpand\csname fornum\string#1\endcsname {\number\numexpr#3}{\ifnum\numexpr#4<0 <\else>\fi}{\number\numexpr#4}\noexpand#1}\fornumtemp } \long\def\fornumi#1#2#3#4#5#6{\def#1{\unless\if...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#PostScript
PostScript
%!PS %%BoundingBox: 0 0 300 200 /size 9 def % defines row * column (9*9 -> 81 numbers,  % from 0 to 80) /itoa { 2 string cvs } bind def % visual bounding box... % 0 0 moveto 300 0 lineto 300 200 lineto 0 200 lineto % closepath stroke 20 150 translate % it can be easily enhanced to support more columns and % ...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#FutureBasic
FutureBasic
  include "NSLog.incl"   NSInteger door, square = 1, increment = 3   for door = 1 to 100 if ( door == square ) NSLog( @"Door %ld is open.", door ) square += increment : increment += 2 else NSLog( @"Door %ld is closed.", door ) end if next   HandleEvents  
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#NetRexx
NetRexx
/* NetRexx */ options replace format comments java crossref symbols nobinary   array = int[10] array[0] = 42   say array[0] array[3] say   words = ['Ogof', 'Ffynnon', 'Ddu']   say words[0] words[1] words[2] say   -- Dynamic arrays can be simulated via the Java Collections package splk = ArrayList() splk.add(words[0]) s...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Quackery
Quackery
[ ' stack nested nested ' share nested join swap nested join dup dup 0 peek put ] is recursive ( x --> x )   [ over 2 < iff [ 2drop 1 ] done dip [ dup 1 - ] do * ] is factorial ( n x --> n )   [ over 2 < iff drop done swap 1 - tuck 1 - over do dip do + ] is fibonacci ( n x --...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#PowerShell
PowerShell
function zigzag( [int] $n ) { $zigzag=New-Object 'Object[,]' $n,$n $nodd = $n -band 1 $nm1 = $n - 1 $i=0; $j=0; foreach( $k in 0..( $n * $n - 1 ) ) { $zigzag[$i,$j] = $k $iodd = $i -band 1 $jodd = $j -band 1 if( ( $j -eq $nm1 ) -and ( $iodd -ne $nodd ) ) { ...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#FUZE_BASIC
FUZE BASIC
READ x,y,z PRINT "Open doors: ";x;" "; CYCLE z=x+y PRINT z;" "; x=z y=y+2 REPEAT UNTIL z>=100 DATA 1,3,0 END
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#NewLISP
NewLISP
(array 5) → (nil nil nil nil nil)
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#R
R
Y <- function(f) { (function(x) { (x)(x) })( function(y) { f( (function(a) {y(y)})(a) ) } ) }
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Racket
Racket
#lang lazy   (define Y (λ (f) ((λ (x) (f (x x))) (λ (x) (f (x x))))))   (define Fact (Y (λ (fact) (λ (n) (if (zero? n) 1 (* n (fact (- n 1)))))))) (define Fib (Y (λ (fib) (λ (n) (if (<= n 1) n (+ (fib (- n 1)) (fib (- n 2))))))))
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Prolog
Prolog
zig_zag(N) :- zig_zag(N, N).   % compute zig_zag for a matrix of Lig lines of Col columns zig_zag(Lig, Col) :- length(M, Lig), maplist(init(Col), M), fill(M, 0, 0, 0, Lig, Col, up), % display the matrix maplist(print_line, M).     fill(M, Cur, L, C, NL, NC, _) :- L is NL - 1, C is NC - 1, nth0(L, M, Line), nt...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#F.C5.8Drmul.C3.A6
Fōrmulæ
Public Sub Main() Dim bDoor As New Boolean[101] Dim siCount1, siCount2, siStart As Short   For siCount1 = 1 To 100 Inc siStart For siCount2 = siStart To 100 Step siCount1 bDoor[siCount2] = Not bDoor[siCount2] Next Next   For siCount1 = 1 To 100 If bDoor[siCount1] Then Print siCount1;; Next   End
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Nim
Nim
var # fixed size arrays x = [1,2,3,4,5,6,7,8,9,10] # type and size automatically inferred y: array[1..5, int] = [1,2,3,4,5] # starts at 1 instead of 0 z: array['a'..'z', int] # indexed using characters   x[0] = x[1] + 1 echo x[0] echo z['d']   x[7..9] = y[3..5] # copy part of array   var # variable size sequences...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Raku
Raku
sub Y (&f) { sub (&x) { x(&x) }( sub (&y) { f(sub ($x) { y(&y)($x) }) } ) } sub fac (&f) { sub ($n) { $n < 2 ?? 1 !! $n * f($n - 1) } } sub fib (&f) { sub ($n) { $n < 2 ?? $n !! f($n - 1) + f($n - 2) } } say map Y($_), ^10 for &fac, &fib;
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#PureBasic
PureBasic
Procedure zigZag(size) Protected i, v, x, y   Dim a(size - 1, size - 1)   x = 1 y = 1 For i = 1 To size * size ;loop once for each element a(x - 1, y - 1) = v ;assign the next index   If (x + y) & 1 = 0 ;even diagonal (zero based count) If x < size ;while inside the squar...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Gambas
Gambas
Public Sub Main() Dim bDoor As New Boolean[101] Dim siCount1, siCount2, siStart As Short   For siCount1 = 1 To 100 Inc siStart For siCount2 = siStart To 100 Step siCount1 bDoor[siCount2] = Not bDoor[siCount2] Next Next   For siCount1 = 1 To 100 If bDoor[siCount1] Then Print siCount1;; Next   End
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#NS-HUBASIC
NS-HUBASIC
10 DIM A(1) 20 A(1)=10 30 PRINT A(1)
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#REBOL
REBOL
Y: closure [g] [do func [f] [f :f] closure [f] [g func [x] [do f :f :x]]]
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Python
Python
def zigzag(n): '''zigzag rows''' def compare(xy): x, y = xy return (x + y, -y if (x + y) % 2 else y) xs = range(n) return {index: n for n, index in enumerate(sorted( ((x, y) for x in xs for y in xs), key=compare ))}     def printzz(myarray): '''show zigzag rows as...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#GAP
GAP
doors := function(n) local a,j,s; a := [ ]; for j in [1 .. n] do a[j] := 0; od; for s in [1 .. n] do j := s; while j <= n do a[j] := 1 - a[j]; j := j + s; od; od; return Filtered([1 .. n], j -> a[j] = 1); end;   doors(100); # [ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#NSIS
NSIS
  !include NSISArray.nsh Function ArrayTest Push $0 ; Declaring an array NSISArray::New TestArray 1 2 NSISArray::Push TestArray "Hello" ; NSISArray arrays are dynamic by default. NSISArray::Push TestArray "World" NSISArray::Read TestArray 1 Pop $0 DetailPrint $0 Pop $0 FunctionEnd  
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#REXX
REXX
/*REXX program implements and displays a stateless Y combinator. */ numeric digits 1000 /*allow big numbers. */ say ' fib' Y(fib (50) ) /*Fibonacci series. */ say ' fib' Y(fib (12 11 10 9 8 7 6 5 ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Quackery
Quackery
[ ]'[ tuck do dip do ] is with2 ( x x --> x x )   [ dup temp put [] swap dup * times [ i^ join ] sortwith [ with2 [ temp share /mod tuck + 1 & if negate ] > ] sortwith [ with2 [ temp share /mod + ] > ] ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Qi
Qi
  (define odd? A -> (= 1 (MOD A 2))) (define even? A -> (= 0 (MOD A 2)))   (define zigzag-val 0 0 N -> 0   X 0 N -> (1+ (zigzag-val (1- X) 0 N)) where (odd? X) X 0 N -> (1+ (zigzag-val (1- X) 1 N))   0 Y N -> (1+ (zigzag-val 1 (1- Y) N)) where (odd? Y) 0 Y N -> (1+ (zigzag-val 0 (1- Y) N))   X Y N -> (1+ (...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#GDScript
GDScript
func Doors(door_count:int) -> void : var doors : Array doors.resize(door_count)   # Note : Initialization is not necessarily mandatory (by default values are false) # Intentionally left here for i in door_count : doors[i] = false   # do visits for i in door_count : for j in range(i,door_count,i+1)...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Oberon-2
Oberon-2
  MODULE Arrays; IMPORT Out;   PROCEDURE Static; VAR x: ARRAY 5 OF LONGINT; BEGIN x[0] := 10; x[1] := 11; x[2] := 12; x[3] := 13; x[4] := x[0];   Out.String("Static at 4: ");Out.LongInt(x[4],0);Out.Ln; END Static;   PROCEDURE Dynamic; VAR x: POINTER TO ARRAY OF LONGINT; ...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Ruby
Ruby
y = lambda do |f| lambda {|g| g[g]}[lambda do |g| f[lambda {|*args| g[g][*args]}] end] end   fac = lambda{|f| lambda{|n| n < 2 ? 1 : n * f[n-1]}} p Array.new(10) {|i| y[fac][i]} #=> [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]   fib = lambda{|f| lambda{|n| n < 2 ? n : f[n-1] + f[n-2]}} p Array.new(10) ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#R
R
zigzag1 <- function(n) { j <- seq(n) u <- rep(c(-1, 1), n) v <- j * (2 * j - 1) - 1 v <- as.vector(rbind(v, v + 1)) a <- matrix(0, n, n) for (i in seq(n)) { a[i, ] <- v[j + i - 1] v <- v + u } a }   zigzag1(5)
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Genie
Genie
  // 100 doors problem // Author: Sinuhe masan (2019) init   // 100 elements array of boolean type doors:bool[100]   for var i = 1 to 100 doors[i] = false // set all doors closed     for var i = 1 to 100 j:int = i while j <= 100 do doors[j] = not doors[j] j = j + i   print("Doors open: ") for var i =...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Objeck
Objeck
  bundle Default { class Arithmetic { function : Main(args : System.String[]), Nil { array := Int->New[2]; array[0] := 13; array[1] := 7; (array[0] + array[1])->PrintLine(); } } }  
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Rust
Rust
  //! A simple implementation of the Y Combinator: //! λf.(λx.xx)(λx.f(xx)) //! <=> λf.(λx.f(xx))(λx.f(xx))   /// A function type that takes its own type as an input is an infinite recursive type. /// We introduce the "Apply" trait, which will allow us to have an input with the same type as self, and break the recursio...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Racket
Racket
  #lang racket   (define/match (compare i j) [((list x y) (list a b)) (or (< x a) (and (= x a) (< y b)))])   (define/match (key i) [((list x y)) (list (+ x y) (if (even? (+ x y)) (- y) y))])   (define (zigzag-ht n) (define indexorder (sort (for*/list ([x n] [y n]) (list x y)) compare #:key key)) (...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Raku
Raku
class Turtle { my @dv = [0,-1], [1,-1], [1,0], [1,1], [0,1], [-1,1], [-1,0], [-1,-1]; my $points = 8; # 'compass' points of neighbors on grid: north=0, northeast=1, east=2, etc.   has @.loc = 0,0; has $.dir = 0; has %.world; has $.maxegg; has $.range-x; has $.range-y;   method turn-...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Glee
Glee
100` *=0=>d $$ create vector 1..100, create bit pattern d, marking all equal to 0 :for (1..100[.s]){ $$ loop s from 1 to 100 d^(100` %s *=0 )=>d;} $$ d = d xor (bit pattern of vector 1..100 % s) d $$ output d  
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Objective-C
Objective-C
// NSArrays are ordered collections of NSObject subclasses only.   // Create an array of NSString objects. NSArray *firstArray = [[NSArray alloc] initWithObjects:@"Hewey", @"Louie", @"Dewey", nil];   // NSArrays are immutable; it does have a mutable subclass, however - NSMutableArray. // Let's instantiate one with a mu...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Scala
Scala
  def Y[A, B](f: (A => B) => (A => B)): A => B = { case class W(wf: W => (A => B)) { def apply(w: W): A => B = wf(w) } val g: W => (A => B) = w => f(w(w))(_) g(W(g)) }  
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Rascal
Rascal
0 (0,0), 1 (0,1), 3 (0,2) 2 (1,0), 4 (1,1), 6 (1,2) 5 (2,0), 7 (2,1), 8 (2,2)
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#REXX
REXX
/*REXX program produces and displays a zig─zag matrix (a square array). */ parse arg n start inc . /*obtain optional arguments from the CL*/ if n=='' | n=="," then n= 5 /*Not specified? Then use the default.*/ if start=='' | start=="," then start= 0 ...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#GML
GML
var doors,a,i; //Sets up the array for all of the doors. for (i = 1; i<=100; i += 1) { doors[i]=0; }   //This first for loop goes through and passes the interval down to the next for loop. for (i = 1; i <= 100; i += 1;) { //This for loop opens or closes the doors and uses the interval(if interval is...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#OCaml
OCaml
# Array.make 6 'A' ;; - : char array = [|'A'; 'A'; 'A'; 'A'; 'A'; 'A'|]   # Array.init 8 (fun i -> i * 10) ;; - : int array = [|0; 10; 20; 30; 40; 50; 60; 70|]   # let arr = [|0; 1; 2; 3; 4; 5; 6 |] ;; val arr : int array = [|0; 1; 2; 3; 4; 5; 6|]   # arr.(4) ;; - : int = 4   # arr.(4) <- 65 ;; - : unit = ()   # arr ;;...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Scheme
Scheme
(define Y ; (Y f) = (g g) where (lambda (f) ; (g g) = (f (lambda a (apply (g g) a))) ((lambda (g) (g g)) ; (Y f) == (f (lambda a (apply (Y f) a))) (lambda (g) (f (lambda a (apply (g g) a)))))))   ;; head-recursive factorial (define fac ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Ring
Ring
  # Project Zig-zag matrix   load "guilib.ring" load "stdlib.ring" new qapp { win1 = new qwidget() { setwindowtitle("Zig-zag matrix") setgeometry(100,100,600,400) n = 5 a = newlist(n,n) zigzag = newlist(n,n) ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Ruby
Ruby
def zigzag(n) (seq=*0...n).product(seq) .sort_by {|x,y| [x+y, (x+y).even? ? y : -y]} .each_with_index.sort.map(&:last).each_slice(n).to_a end   def print_matrix(m) format = "%#{m.flatten.max.to_s.size}d " * m[0].size puts m.map {|row| format % row} end   print_matrix zigzag(5)
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Go
Go
package main   import "fmt"   func main() { doors := [100]bool{}   // the 100 passes called for in the task description for pass := 1; pass <= 100; pass++ { for door := pass-1; door < 100; door += pass { doors[door] = !doors[door] } }   // one more pass to answer the ques...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Oforth
Oforth
[ "abd", "def", "ghi" ] at( 3 ) .   Array new dup addAll( [1, 2, 3] ) dup put( 2, 8.1 ) .  
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Shen
Shen
(define y F -> ((/. X (X X)) (/. X (F (/. Z ((X X) Z))))))   (let Fac (y (/. F N (if (= 0 N) 1 (* N (F (- N 1)))))) (output "~A~%~A~%~A~%" (Fac 0) (Fac 5) (Fac 10)))
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Sidef
Sidef
var y = ->(f) {->(g) {g(g)}(->(g) { f(->(*args) {g(g)(args...)})})}   var fac = ->(f) { ->(n) { n < 2 ? 1 : (n * f(n-1)) } } say 10.of { |i| y(fac)(i) }   var fib = ->(f) { ->(n) { n < 2 ? n : (f(n-2) + f(n-1)) } } say 10.of { |i| y(fib)(i) }
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Rust
Rust
  use std::cmp::Ordering; use std::cmp::Ordering::{Equal, Greater, Less}; use std::iter::repeat;   #[derive(Debug, PartialEq, Eq)] struct SortIndex { x: usize, y: usize, }   impl SortIndex { fn new(x: usize, y: usize) -> SortIndex { SortIndex { x, y } } }   impl PartialOrd for SortIndex { fn...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Scala
Scala
def zigzag(n: Int): Array[Array[Int]] = { val l = for (i <- 0 until n*n) yield (i%n, i/n) val lSorted = l.sortWith { case ((x,y), (u,v)) => if (x+y == u+v) if ((x+y) % 2 == 0) x<u else y<v else x+y < u+v } val res = Array.ofDim[Int](n, n) lSorted.zipWithIndex fo...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Golfscript
Golfscript
100:c;[{0}c*]:d; c,{.c,>\)%{.d<\.d=1^\)d>++:d;}/}/ [c,{)"door "\+" is"+}%d{{"open"}{"closed"}if}%]zip {" "*puts}/
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Ol
Ol
  ; making a vector > #(1 2 3 4 5) #(1 2 3 4 5)   ; making a vector in a functional way > (vector 1 2 3 4 5) #(1 2 3 4 5)   ; another functional vector making way > (make-vector '(1 2 3 4 5)) #(1 2 3 4 5)   ; the same as above functional vector making way > (list->vector '(1 2 3 4 5)) #(1 2 3 4 5)   ; modern syntax of ...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Slate
Slate
Method traits define: #Y &builder: [[| :f | [| :x | f applyWith: (x applyWith: x)] applyWith: [| :x | f applyWith: (x applyWith: x)]]].
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Scilab
Scilab
function a = zigzag3(n) a = zeros(n, n) for k=1:n j = modulo(k, 2) d = (2*j-1)*(n-1) m = (n-1)*(k-1) a(k+(1-j)*m:d:k+j*m) = k*(k-1)/2:k*(k+1)/2-1 a(n*(n+1-k)+(1-j)*m:d:n*(n+1-k)+j*m) = n*n-k*(k+1)/2:n*n-k*(k-1)/2-1 end endfunction   -->zigzag3(5) ans =   0. 1. 5. 6. 14. ...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Gosu
Gosu
  uses java.util.Arrays   var doors = new boolean[100] Arrays.fill( doors, false )   for( pass in 1..100 ) { var counter = pass-1 while( counter < 100 ) { doors[counter] = !doors[counter] counter += pass } }   for( door in doors index i ) { print( "door ${i+1} is ${door ? 'open' : 'closed'...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#ooRexx
ooRexx
a = .array~new -- create a zero element array b = .array~new(10) -- create an array with initial size of 10 c = .array~of(1, 2, 3) -- create a 3 element array holding objects 1, 2, and 3 a[3] = "Fred" -- assign an item b[2] = a[3] -- retrieve an item from the array c~append(4)...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Smalltalk
Smalltalk
Y := [:f| [:x| x value: x] value: [:g| f value: [:x| (g value: g) value: x] ] ].   fib := Y value: [:f| [:i| i <= 1 ifTrue: [i] ifFalse: [(f value: i-1) + (f value: i-2)] ] ].   (fib value: 10) displayNl.   fact := Y value: [:f| [:i| i = 0 ifTrue: [1] ifFalse: [(f value: i-1) * i] ] ].   (fact value: 10) displayNl.
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Seed7
Seed7
$ include "seed7_05.s7i";   const type: matrix is array array integer;   const func matrix: zigzag (in integer: size) is func result var matrix: s is matrix.value; local var integer: i is 1; var integer: j is 1; var integer: d is -1; var integer: max is 0; var integer: n is 0; begin s ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Sidef
Sidef
func zig_zag(w, h) {   var r = [] var n = 0   h.of { |e| w.of { |f| [e, f] } }.reduce('+').sort { |a, b| (a[0]+a[1] <=> b[0]+b[1]) || (a[0]+a[1] -> is_even ? a[0]<=>b[0]  : a[1]<=>b[1]) }.each { |a| r[a[1]][a[0]...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Groovy
Groovy
doors = [false] * 100 (0..99).each { it.step(100, it + 1) { doors[it] ^= true } } (0..99).each { println("Door #${it + 1} is ${doors[it] ? 'open' : 'closed'}.") }
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#OxygenBasic
OxygenBasic
    'CREATING A STATIC ARRAY float f[100]   'SETTING INDEX BASE indexbase 1 'default   'FILLING PART OF AN ARRAY f[20]={2,4,6,8,10,12}   'MAPPING AN ARRAY TO ANOTHER float *g @g=@f[20] print g[6] 'result 12   'DYNAMIC (RESIZEABLE) ARRAYS redim float f(100) f={2,4,6,8} 'assign some values redim float f(200...
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#Standard_ML
Standard ML
- datatype 'a mu = Roll of ('a mu -> 'a) fun unroll (Roll x) = x   fun fix f = (fn x => fn a => f (unroll x x) a) (Roll (fn x => fn a => f (unroll x x) a))   fun fac f 0 = 1 | fac f n = n * f (n-1)   fun fib f 0 = 0 | fib f 1 = 1 | fib f n = f (n-1) + f (n-2) ; datatype 'a mu = Roll of 'a mu -> 'a v...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Standard_ML
Standard ML
fun rowprint r = (List.app (fn i => print (StringCvt.padLeft #" " 3 (Int.toString i))) r; print "\n"); fun zig lst M = List.app rowprint (lst M);   fun sign t = if t mod 2 = 0 then ~1 else 1;   fun zag n = List.tabulate (n, fn i=> rev ( List.tabulate (n, fn j => ...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Stata
Stata
function zigzag1(n) { j = 0::n-1 u = J(1, n, (-1, 1)) v = (j:*(2:*j:+3)) v = rowshape((v,v:+1), 1) a = J(n, n, .) for (i=1; i<=n; i++) { a[i, .] = v[j:+i] v = v+u } return(a) }   zigzag1(5) 1 2 3 4 5 +--------------------------+ 1 | 0 1 5 6 14 | 2 | 2 4 7 ...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#GW-BASIC
GW-BASIC
10 DIM A(100) 20 FOR OFFSET = 1 TO 100 30 FOR I = 0 TO 100 STEP OFFSET 40 A(I) = A(I) + 1 50 NEXT I 60 NEXT OFFSET 70 ' Print "opened" doors 80 FOR I = 1 TO 100 90 IF A(I) MOD 2 = 1 THEN PRINT I 100 NEXT I
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Oz
Oz
declare Arr = {Array.new 1 %% lowest index 10 %% highest index 37} %% all 10 fields initialized to 37 in {Show Arr.1} Arr.1 := 64 {Show Arr.1}
http://rosettacode.org/wiki/Y_combinator
Y combinator
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's stat...
#SuperCollider
SuperCollider
// z-combinator ( z = { |f| { |x| x.(x) }.( { |y| f.({ |args| y.(y).(args) }) } ) }; )   // the same in a shorter form   ( r = { |x| x.(x) }; z = { |f| r.({ |y| f.(r.(y).(_)) }) }; )     // factorial k = { |f| { |x| if(x < 2, 1, { x * f.(x - 1) }) } };   g = z.(k);   g.(5) // 120   (1..10).collect(g) // [ 1, 2...
http://rosettacode.org/wiki/Zig-zag_matrix
Zig-zag matrix
Task Produce a zig-zag array. A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images). For example, gi...
#Tcl
Tcl
proc zigzag {size} { set m [lrepeat $size [lrepeat $size .]] set x 0; set dx -1 set y 0; set dy 1   for {set i 0} {$i < $size ** 2} {incr i} { if {$x >= $size} { incr x -1 incr y 2 negate dx dy } elseif {$y >= $size} { incr x 2 ...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#Harbour
Harbour
#define ARRAY_ELEMENTS 100 PROCEDURE Main() LOCAL aDoors := Array( ARRAY_ELEMENTS ) LOCAL i, j   AFill( aDoors, .F. ) FOR i := 1 TO ARRAY_ELEMENTS FOR j := i TO ARRAY_ELEMENTS STEP i aDoors[ j ] = ! aDoors[ j ] NEXT NEXT AEval( aDoors, {|e, n| QQout( Padl(n,3) + " is " + Iif(aDoor...