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/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... | #Elixir | Elixir | defmodule HundredDoors do
def doors(n \\ 100) do
List.duplicate(false, n)
end
def toggle(doors, n) do
List.update_at(doors, n, &(!&1))
end
def toggle_every(doors, n) do
Enum.reduce( Enum.take_every((n-1)..99, n), doors, fn(n, acc) -> toggle(acc, n) end )
end
end
# unoptimized
final_state =... |
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,... | #LFE | LFE |
; Create a fixed-size array with entries 0-9 set to 'undefined'
> (set a0 (: array new 10))
#(array 10 0 undefined 10)
> (: array size a0)
10
; Create an extendible array and set entry 17 to 'true',
; causing the array to grow automatically
> (set a1 (: array set 17 'true (: array new)))
#(array
18
...
(: array... |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Racket | Racket | #lang racket
(require racklog)
(define %select
(%rel (x xs S S1)
[(x (cons x xs) xs)]
[(x (cons S xs) (cons S S1)) (%select x xs S1)]
[((cons x xs) S)
(%select x S S1)
(%select xs S1)]
[('() (_))]))
(define %next-to
(%rel (A B C)
[(A B C)
(%or (%left-of A B C)
(%le... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Rascal | Rascal | import util::Math;
import vis::Render;
import vis::Figure;
public void yinyang(){
template = ellipse(fillColor("white"));
smallWhite = ellipse(fillColor("white"), shrink(0.1), valign(0.75));
smallBlack = ellipse(fillColor("black"), shrink(0.1), valign(0.25));
dots= [ellipse(fillColor("white"), shrink(0.000001... |
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... | #Julia | Julia |
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.6.3 (2021-09-23)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
... |
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... | #Ksh | Ksh |
#!/bin/ksh
# Produce a zig-zag array.
# # Variables:
#
integer DEF_SIZE=5 # Default size = 5
arr_size=${1:-$DEF_SIZE} # $1 = size, or default
# # Externals:
#
# # Functions:
#
######
# main #
######
integer i j n
typeset -a zzarr
for (( i=n=0; i<arr_size*2; i++ )); do
for (( j= (i<arr_size)... |
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... | #Lasso | Lasso |
var(
'square' = array
,'size' = integer( 5 )// for a 5 X 5 square
,'row' = array
,'x' = integer( 1 )
,'y' = integer( 1 )
,'counter' = integer( 1 )
);
// create place-holder matrix
loop( $size );
$row = array;
loop( $size );
$row->insert( 0 );
/loop;
... |
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... | #Elm | Elm | -- Unoptimized
import List exposing (indexedMap, foldl, repeat, range)
import Html exposing (text)
import Debug exposing (toString)
type Door = Open | Closed
toggle d = if d == Open then Closed else Open
toggleEvery : Int -> List Door -> List Door
toggleEvery k doors = indexedMap
(\i door -> if modBy k (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,... | #Liberty_BASIC | Liberty BASIC |
dim Array(10)
Array(0) = -1
Array(10) = 1
print Array( 0), Array( 10)
REDIM Array( 100)
print Array( 0), Array( 10)
Array( 0) = -1
print Array( 0), Array( 10)
|
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Raku | Raku | my Hash @houses = (1 .. 5).map: { %(:num($_)) }; # 1 there are five houses
my @facts = (
{ :nat<English>, :color<red> }, # 2 The English man lives in the red house.
{ :nat<Swede>, :pet<dog> }, # 3 The Swede has a dog.
{ :nat<Dane>, :drink<tea> }, # 4 The Dane drinks tea.
{ :color... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #REXX | REXX | /*REXX program creates & displays an ASCII art version of the Yin─Yang (taijitu) symbol.*/
parse arg s1 s2 . /*obtain optional arguments from the CL*/
if s1=='' | s1=="," then s1= 17 /*Not defined? Then use the default. */
if s2=='' | s2=="," then s2= s1 % 2 ... |
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... | #Kitten | Kitten | define y<S..., T...> (S..., (S..., (S... -> T...) -> T...) -> T...):
-> f; { f y } f call
define fac (Int32, (Int32 -> Int32) -> Int32):
-> x, rec;
if (x <= 1) { 1 } else { (x - 1) rec call * x }
define fib (Int32, (Int32 -> Int32) -> Int32):
-> x, rec;
if (x <= 2):
1
else:
(x - 1) rec call -> a... |
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... | #Lua | Lua |
local zigzag = {}
function zigzag.new(n)
local a = {}
local i -- cols
local j -- rows
a.n = n
a.val = {}
for j = 1, n do
a.val[j] = {}
for i = 1, n do
a.val[j][i] = 0
end
end
i = 1
j = 1
local di
local dj
local k = 0
w... |
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... | #Emacs_Lisp | Emacs Lisp | (defun create-doors ()
"Returns a list of closed doors
Each door only has two status: open or closed.
If a door is closed it has the value 0, if it's open it has the value 1."
(let ((return_value '(0))
;; There is already a door in the return_value, so k starts at 1
;; otherwise we would need to... |
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,... | #LIL | LIL | # (not) Arrays, in LIL
set a [list abc def ghi]
set b [list 4 5 6]
print [index $a 0]
print [index $b 1]
print [count $a]
append b [list 7 8 9]
print [count $b]
print $b |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #REXX | REXX | /* REXX ---------------------------------------------------------------
* Solve the Zebra Puzzle
*--------------------------------------------------------------------*/
Call mk_perm /* compute all permutations */
Call encode /* encode the elements of the specifications */
/* ex... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Ruby | Ruby | Shoes.app(:width => 470, :height => 380) do
PI = Shoes::TWO_PI/2
strokewidth 1
def yin_yang(x, y, radius)
fill black; stroke black
arc x, y, radius, radius, -PI/2, PI/2
fill white; stroke white
arc x, y, radius, radius, PI/2, -PI/2
oval x-radius/4, y-radius/2, radius/2-1
fill black... |
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... | #Klingphix | Klingphix | :fac
dup 1 great [dup 1 sub fac mult] if
;
:fib
dup 1 great [dup 1 sub fib swap 2 sub fib add] if
;
:test
print ": " print
10 [over exec print " " print] for
nl
;
@fib "fib" test
@fac "fac" test
"End " input |
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... | #M4 | M4 | divert(-1)
define(`set2d',`define(`$1[$2][$3]',`$4')')
define(`get2d',`defn(`$1[$2][$3]')')
define(`for',
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),$3,`$4')')')')
define(`show2d',
`for(`x',0,decr($2),
`for(`y',0,decr($3),`format(`%2d',get2d($1,x,y... |
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... | #Erlang | Erlang |
-module(hundoors).
-export([go/0]).
toggle(closed) -> open;
toggle(open) -> closed.
go() -> go([closed || _ <- lists:seq(1, 100)],[], 1, 1).
go([], L, N, _I) when N =:= 101 -> lists:reverse(L);
go([], L, N, _I) -> go(lists:reverse(L), [], N + 1, 1);
go([H|T], L, N, I) ->
H2 = case I rem N of
0 -> toggle(H... |
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,... | #Lingo | Lingo | a = [1,2] -- or: a = list(1,2)
put a[2] -- or: put a.getAt(2)
-- 2
a.append(3)
put a
-- [1, 2, 3]
a.deleteAt(2)
put a
-- [1, 3]
a[1] = 5 -- or: a.setAt(1, 5)
put a
-- [5, 3]
a.sort()
put a
-- [3, 5] |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Ruby | Ruby | CONTENT = { House: '',
Nationality: %i[English Swedish Danish Norwegian German],
Colour: %i[Red Green White Blue Yellow],
Pet: %i[Dog Birds Cats Horse Zebra],
Drink: %i[Tea Coffee Milk Beer Water],
Smoke: %i[PallMall Dunhill Blue... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Rust | Rust | use svg::node::element::Path;
fn main() {
let doc = svg::Document::new()
.add(yin_yang(15.0, 1.0).set("transform", "translate(20,20)"))
.add(yin_yang(6.0, 1.0).set("transform", "translate(50,11)"));
svg::save("yin_yang.svg", &doc).unwrap();
}
/// th - the thickness of the outline around yang
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... | #Kotlin | Kotlin | // version 1.1.2
typealias Func<T, R> = (T) -> R
class RecursiveFunc<T, R>(val p: (RecursiveFunc<T, R>) -> Func<T, R>)
fun <T, R> y(f: (Func<T, R>) -> Func<T, R>): Func<T, R> {
val rec = RecursiveFunc<T, R> { r -> f { r.p(r)(it) } }
return rec.p(rec)
}
fun fac(f: Func<Int, Int>) = { x: Int -> if (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... | #Maple | Maple | zigzag1:=proc(n)
uses ArrayTools;
local i,u,v,a;
u:=Replicate(<-1,1>,n):
v:=Vector[row](1..n,i->i*(2*i-3)):
v:=Reshape(<v+~1,v+~2>,2*n):
a:=Matrix(n,n):
for i to n do
a[...,i]:=v[i+1..i+n];
v+=u
od:
a
end:
zigzag2:=proc(n)
local i,v,a;
a:=zigzag1(n);
v:=Vector(1..n-1,i->i^2);
for i f... |
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... | #ERRE | ERRE |
! "100 Doors" program for ERRE LANGUAGE
! Author: Claudio Larini
! Date: 21-Nov-2014
!
! PC Unoptimized version translated from a QB version
PROGRAM 100DOORS
!$INTEGER
CONST N=100
DIM DOOR[N]
BEGIN
FOR STRIDE=1 TO N DO
FOR INDEX=STRIDE TO N STEP STRIDE DO
DOOR[INDEX]=NOT(DOOR[INDEX])
END F... |
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,... | #Lisaac | Lisaac | + a : ARRAY(INTEGER);
a := ARRAY(INTEGER).create 0 to 9;
a.put 1 to 0;
a.put 3 to 1;
a.item(1).print; |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Scala | Scala | /* Note to the rules:
*
* It can further concluded that:
* 5a: The green house cannot be at the h1 position
* 5b: The white house cannot be at the h5 position
*
* 16: This rule is redundant.
*/
object Einstein extends App {
val possibleMembers = for { // pair clues results in 78 members
nationality <- ... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Scala | Scala | import scala.swing.Swing.pair2Dimension
import scala.swing.{ MainFrame, Panel }
import java.awt.{ Color, Graphics2D }
object YinYang extends scala.swing.SimpleSwingApplication {
var preferedSize = 500
/** Draw a Taijitu symbol on the given graphics context.
*/
def drawTaijitu(g: Graphics2D, size: Int) {
... |
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... | #Lambdatalk | Lambdatalk |
1) defining the Ycombinator
{def Y {lambda {:f} {:f :f}}}
2) defining non recursive functions
2.1) factorial
{def almost-fac
{lambda {:f :n}
{if {= :n 1}
then 1
else {* :n {:f :f {- :n 1}}}}}}
2.2) fibonacci
{def almost-fibo
{lambda {:f :n}
{if {< :n 2}
then 1
else {+ {:f :f {- :n 1}} {:f :f... |
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... | #Mathematica_.2F_Wolfram_Language | Mathematica / Wolfram Language | ZigZag[size_Integer/;size>0]:=Module[{empty=ConstantArray[0,{size,size}]},
empty=ReplacePart[empty,{i_,j_}:>1/2 (i+j)^2-(i+j)/2-i (1-Mod[i+j,2])-j Mod[i+j,2]];
ReplacePart[empty,{i_,j_}/;i+j>size+1:> size^2-tmp[[size-i+1,size-j+1]]-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... | #Euler_Math_Toolbox | Euler Math Toolbox |
>function Doors () ...
$ doors:=zeros(1,100);
$ for i=1 to 100
$ for j=i to 100 step i
$ doors[j]=!doors[j];
$ end;
$ end;
$ return doors
$endfunction
>nonzeros(Doors())
[ 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,... | #Little | Little | String fruit[] = {"apple", "orange", "Pear"} |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Sidef | Sidef | var CONTENT = Hash(
:House => nil,
:Nationality => [:English, :Swedish, :Danish, :Norwegian, :German],
:Colour => [:Red, :Green, :White, :Blue, :Yellow],
:Pet => [:Dog, :Birds, :Cats, :Horse, :Zebra],
:Drink => [:Tea, :Coffee, :Milk, :... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Scilab | Scilab | R = 1; //outer radius of first image
scale = 0.5; //scale of the second image
scf(0); clf();
set(gca(),'isoview','on');
xname('Yin and Yang');
//First one
n_p = 100; //number of points per arc
angles = []; //angles for each arc
angles = linspace(%p... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Seed7 | Seed7 | $ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
include "draw.s7i";
include "keybd.s7i";
const proc: yinYang (in integer: xPos, in integer: yPos, in integer: size) is func
begin
pieslice(xPos, yPos, size, 3.0 * PI / 2.0, PI, black);
pieslice(xPos, yPos, size, PI / 2.0, PI, white);... |
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... | #Lua | Lua | Y = function (f)
return function(...)
return (function(x) return x(x) end)(function(x) return f(function(y) return x(x)(y) end) end)(...)
end
end
|
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... | #MATLAB | MATLAB | function matrix = zigZag(n)
%This is very unintiutive. This algorithm parameterizes the
%zig-zagging movement along the matrix indicies. The easiest way to see
%what this algorithm does is to go through line-by-line and write out
%what the algorithm does on a peace of paper.
matrix = zeros(n);
... |
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... | #Euphoria | Euphoria | -- doors.ex
include std/console.e
sequence doors
doors = repeat( 0, 100 ) -- 1 to 100, initialised to false
for pass = 1 to 100 do
for door = pass to 100 by pass do
--printf( 1, "%d", doors[door] )
--printf( 1, "%d", not doors[door] )
doors[door] = not doors[door]
end for
end for
sequence oc
for i = 1 to... |
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,... | #Logo | Logo | array 5 ; default origin is 1, every item is empty
(array 5 0) ; custom origin
make "a {1 2 3 4 5} ; array literal
setitem 1 :a "ten ; Logo is dynamic; arrays can contain different types
print item 1 :a ; ten |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Standard_ML | Standard ML |
(* Attributes and values *)
val str_attributes = Vector.fromList ["Color", "Nation", "Drink", "Pet", "Smoke"]
val str_colors = Vector.fromList ["Red", "Green", "White", "Yellow", "Blue"]
val str_nations = Vector.fromList ["English", "Swede", "Dane", "German", "Norwegian"]
val str_... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Sidef | Sidef | func circle (rad, cx, cy, fill='white', stroke='black') {
say "<circle cx='#{cx}' cy='#{cy}' r='#{rad}' fill='#{fill}' stroke='#{stroke}' stroke-width='1'/>";
}
func yin_yang (rad, cx, cy, fill='white', stroke='black', angle=90) {
var (c, w) = (1, 0);
angle != 0 && say "<g transform='rotate(#{angle}, #{cx... |
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... | #M2000_Interpreter | M2000 Interpreter |
Module Ycombinator {
\\ y() return value. no use of closure
y=lambda (g, x)->g(g, x)
Print y(lambda (g, n as decimal)->if(n=0->1, n*g(g, n-1)), 10)=3628800 ' true
Print y(lambda (g, n)->if(n<=1->n,g(g, n-1)+g(g, n-2)), 10)=55 ' true
\\ Using closure in y, y() return function
y=lambda (g)->lambda g (x) -> g(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... | #Maxima | Maxima | zigzag(n) := block([a, i, j],
a: zeromatrix(n, n),
i: 1,
j: 1,
for k from 0 thru n*n - 1 do (
a[i, j]: k,
if evenp(i + j) then (
if j < n then j: j + 1 else i: i + 2,
if i > 1 then i: i - 1
) else (
if i < n then i: i + 1 else j: j + 2,
if j > 1 then j: j - 1
)
),
a)$
zigzag(... |
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... | #Excel | Excel |
=IF($A2/C$1=INT($A2/C$1),IF(B2=0,1,IF(B2=1,0)),B2)
|
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,... | #LOLCODE | LOLCODE | HAI 1.4
BTW declaring array
I HAS A array ITZ A BUKKIT
BTW store values in array
array HAS A one ITZ 1
array HAS A two ITZ 2
array HAS A three ITZ 3
array HAS A string ITZ "MEOW"
OBTW
there is no way to get an element of an array at a specified index in LOLCODE
therefore, you can't really iterate through an array
TLD... |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Tailspin | Tailspin |
processor EinsteinSolver
@: [{|{}|}]; // A list of possible relations, start with one relation with an empty tuple
$ -> \(
when <[](1..)> do
def variableRange: $(1);
@EinsteinSolver: [($@EinsteinSolver(1) join {|{by $variableRange...}|})];
$(2..last) -> #
\) -> !VOID
sink isFact
de... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #SVG_3 | SVG | <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="600" height="600">
<!-- We create the symbol in t... |
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... | #MANOOL | MANOOL |
{ {extern "manool.org.18/std/0.3/all"} in
: let { Y = {proc {F} as {proc {X} as X[X]}[{proc {X} with {F} as F[{proc {Y} with {X} as X[X][Y]}]}]} } in
{ for { N = Range[10] } do
: (WriteLine) Out; N "! = "
{Y: proc {Rec} as {proc {N} with {Rec} as: if N == 0 then 1 else N * Rec[N - 1]}}$[N]
}
{ for { N = R... |
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... | #Maple | Maple |
> Y:=f->(x->x(x))(g->f((()->g(g)(args)))):
> Yfac:=Y(f->(x->`if`(x<2,1,x*f(x-1)))):
> seq( Yfac( i ), i = 1 .. 10 );
1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800
> Yfib:=Y(f->(x->`if`(x<2,x,f(x-1)+f(x-2)))):
> seq( Yfib( i ), i = 1 .. 10 );
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
|
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... | #MiniZinc | MiniZinc |
%Zigzag Matrix. Nigel Galloway, February 3rd., 2020
int: Size;
array [1..Size,1..Size] of var 1..Size*Size: zigzag;
constraint zigzag[1,1]=1 /\ zigzag[Size,Size]=Size*Size;
constraint forall(n in {2*g | g in 1..Size div 2})(zigzag[1,n]=zigzag[1,n-1]+1 /\ forall(g in 2..n)(zigzag[g,n-g+1]=zigzag[g-1,n-g+2]+1));
constr... |
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.23 | F# | let answerDoors =
let ToggleNth n (lst:bool array) = // Toggle every n'th door
[(n-1) .. n .. 99] // For each appropriate door
|> Seq.iter (fun i -> lst.[i] <- not lst.[i]) // toggle it
let doors = Array.create 100 false // Initial... |
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,... | #LSE64 | LSE64 | 10 myArray :array
0 array 5 [] ! # store 0 at the sixth cell in the array
array 5 [] @ # contents of sixth cell in array |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Tcl | Tcl | package require struct::list
# Implements the constants by binding them directly into the named procedures.
# This is much faster than the alternatives!
proc initConstants {args} {
global {}
set remap {}
foreach {class elems} {
Number {One Two Three Four Five}
Color {Red Green Blue White Yellow}
Drink ... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Tcl | Tcl | package require Tcl 8.5
package require Tk
namespace import tcl::mathop::\[-+\] ;# Shorter coordinate math
proc yinyang {c x y r {colors {white black}}} {
lassign $colors a b
set tt [expr {$r * 2 / 3.0}]
set h [expr {$r / 2.0}]
set t [expr {$r / 3.0}]
set s [expr {$r / 6.0}]
$c create arc [... |
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... | #Mathematica_.2F_Wolfram_Language | Mathematica / Wolfram Language | Y = Function[f, #[#] &[Function[g, f[g[g][##] &]]]];
factorial = Y[Function[f, If[# < 1, 1, # f[# - 1]] &]];
fibonacci = Y[Function[f, If[# < 2, #, f[# - 1] + f[# - 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... | #Modula-3 | Modula-3 | MODULE ZigZag EXPORTS Main;
IMPORT IO, Fmt;
TYPE Matrix = REF ARRAY OF ARRAY OF CARDINAL;
PROCEDURE Create(size: CARDINAL): Matrix =
PROCEDURE move(VAR i, j: INTEGER) =
BEGIN
IF j < (size - 1) THEN
IF (i - 1) < 0 THEN
i := 0;
ELSE
i := i - 1;
END;
IN... |
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... | #Factor | Factor | USING: bit-arrays formatting fry kernel math math.ranges
sequences ;
IN: rosetta.doors
CONSTANT: number-of-doors 100
: multiples ( n -- range )
0 number-of-doors rot <range> ;
: toggle-multiples ( n doors -- )
[ multiples ] dip '[ _ [ not ] change-nth ] each ;
: toggle-all-multiples ( doors -- )
[ n... |
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,... | #LSL | LSL |
default {
state_entry() {
list lst = ["1", "2", "3"];
llSay(0, "Create and Initialize a List\nList=["+llList2CSV(lst)+"]\n");
lst += ["A", "B", "C"];
llSay(0, "Append to List\nList=["+llList2CSV(lst)+"]\n");
lst = llListInsertList(lst, ["4", "5", "6"], 3);
llSay... |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #VBA | VBA | Option Base 1
Public Enum attr
Colour = 1
Nationality
Beverage
Smoke
Pet
End Enum
Public Enum Drinks_
Beer = 1
Coffee
Milk
Tea
Water
End Enum
Public Enum nations
Danish = 1
English
German
Norwegian
Swedish
End Enum
Public Enum colors
Blue = 1
Green
... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #UNIX_Shell | UNIX Shell | #!/usr/bin/env bash
in_circle() { #(cx, cy, r, x y)
# return true if the point (x,y) lies within the circle of radius r centered
# on (cx,cy)
# (but really scaled to an ellipse with vertical minor semiaxis r and
# horizontal major semiaxis 2r)
local -i cx=$1 cy=$2 r=$3 x=$4 y=$5
local -i dx dy
(( dx=(x-cx... |
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... | #Moonscript | Moonscript | Z = (f using nil) -> ((x) -> x x) (x) -> f (...) -> (x x) ...
factorial = Z (f using nil) -> (n) -> if n == 0 then 1 else n * f 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... | #NetRexx | NetRexx | /* NetRexx */
options replace format comments java crossref savelog symbols binary
zigzag(5)
return
method zigzag(msize) public static
row = 1
col = 1
ziggy = Rexx(0)
loop j_ = 0 for msize * msize
ziggy[row, col] = j_
if (row + col) // 2 == 0 then do
if col < msize then -
col = c... |
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... | #Falcon | Falcon | doors = arrayBuffer( 101, false )
for pass in [ 0 : doors.len() ]
for door in [ 0 : doors.len() : pass+1 ]
doors[ door ] = not doors[ door ]
end
end
for door in [ 1 : doors.len() ] // Show Output
> "Door ", $door, " is: ", ( doors[ door ] ) ? "open" : "closed"
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,... | #Lua | Lua | l = {}
l[1] = 1 -- Index starts with 1, not 0.
l[0] = 'zero' -- But you can use 0 if you want
l[10] = 2 -- Indexes need not be continuous
l.a = 3 -- Treated as l['a']. Any object can be used as index
l[l] = l -- Again, any object can be used as an index. Even other tables
for i,v in next,l do print ... |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Vlang | Vlang | type HouseSet = []House
struct House {
n Nationality
c Colour
a Animal
d Drink
s Smoke
}
// Define the possible values
enum Nationality {
english = 0
swede
dane
norwegian
german
}
enum Colour {
red = 0
green
white
yellow
blue
}
enum Animal {
dog = 0
... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #Wren | Wren | var inCircle = Fn.new { |centerX, centerY, radius, x, y|
return (x-centerX)*(x-centerX)+(y-centerY)*(y-centerY) <= radius*radius
}
var inBigCircle = Fn.new { |radius, x, y| inCircle.call(0, 0, radius, x, y) }
var inBlackSemiCircle = Fn.new { |radius, x, y| inCircle.call(0, -radius/2, radius/2, x, 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... | #Nim | Nim | # The following is implemented for a strict language as a Z-Combinator;
# Z-combinators differ from Y-combinators in lacking one Beta reduction of
# the extra `T` argument to the function to be recursed...
import sugar
proc fixz[T, TResult](f: ((T) -> TResult) -> ((T) -> TResult)): (T) -> TResult =
type Recursive... |
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... | #Nim | Nim | from algorithm import sort
from strutils import align
from sequtils import newSeqWith
type Pos = tuple[x, y: int]
proc `<` (a, b: Pos): bool =
a.x + a.y < b.x + b.y or
a.x + a.y == b.x + b.y and (a.x < b.x xor (a.x + a.y) mod 2 == 0)
proc zigzagMatrix(n: int): auto =
var indices = newSeqOfCap[Pos](n*n)
... |
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... | #FALSE | FALSE | 100[$][0 1ø:1-]# {initialize doors}
%
[s;[$101\>][$$;~\:s;+]#%]d: {function d, switch door state function}
1s:[s;101\>][d;!s;1+s:]# {increment step width from 1 to 100, execute function d each time}
1[$101\>][$$." ";$["open
"]?~["closed
"]?1+]# {loop through doors, print door n... |
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,... | #M2000_Interpreter | M2000 Interpreter |
Module CheckArray {
\\ Array with parenthesis in name
Dim A(10)=1
Global B(10)=1
For This {
Local A(10)=5
Print A(4)=5
}
Print A(4)=1
\\ Auto Array
M=(1,2,3,4,5)
Link M to M()
Print M(2)=3
Return M, 0:=100, 5-4:=300
\\... |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #Wren | Wren | import "/fmt" for Fmt
var colors = ["Red", "Green", "White", "Yellow", "Blue"]
var nations = ["English", "Swede", "Danish", "Norwegian", "German"]
var animals = ["Dog", "Birds", "Cats", "Horse", "Zebra"]
var drinks = ["Tea", "Coffee", "Milk", "Beer", "Water"]
var smokes = ["Pall Mall", "Dunhill", "Blend", "Blue Ma... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #XPL0 | XPL0 | include c:\cxpl\codes; \intrinsic 'code' declarations
def Black=0, Red=4, White=$F;
proc Circle(X0, Y0, R, CL, CR); \Show a filled circle
int X0, Y0, R, CL, CR; \left and right half colors
int X, Y;
[for Y:= -R to R do
for X:= -R to R do
if X*X + Y*Y <= R*R then
Point(X+X0,... |
http://rosettacode.org/wiki/Yin_and_yang | Yin and yang | One well-known symbol of the philosophy of duality known as yin and yang is the taijitu.
Task
Create a function that, given a parameter representing size, generates such a symbol scaled to the requested size.
Generate and display the symbol for two different (small) sizes.
| #zkl | zkl | fcn draw_yinyang(trans,scale){
0'|<use xlink:href="#y" transform="translate(%d,%d) scale(%g)"/>|
.fmt(trans,trans,scale).print();
}
print(
"<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n"
"<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'\n"
" 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>\n"
"<svg ... |
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... | #Objective-C | Objective-C | #import <Foundation/Foundation.h>
typedef int (^Func)(int);
typedef Func (^FuncFunc)(Func);
typedef Func (^RecursiveFunc)(id); // hide recursive typing behind dynamic typing
Func Y(FuncFunc f) {
RecursiveFunc r =
^(id y) {
RecursiveFunc w = y; // cast value back into desired type
return f(^(int 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... | #Objeck | Objeck |
function : native : ZigZag(size : Int) ~ Int[,] {
data := Int->New[size, size];
i := 1;
j := 1;
max := size * size;
for(element := 0; element < max ; element += 1;) {
data[i - 1, j - 1] := element;
if((i + j) % 2 = 0) {
# even stripes
if(j < size){
j += 1;
}
else{... |
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... | #Fantom | Fantom |
states := (1..100).toList
100.times |i| {
states = states.map |state| { state % (i+1) == 0 ? -state : +state }
}
echo("Open doors are " + states.findAll { it < 0 }.map { -it })
|
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,... | #Maple | Maple | #defining an array of a certain length
a := Array (1..5);
a := [ 0 0 0 0 0 ]
#can also define with a list of entries
a := Array ([1, 2, 3, 4, 5]);
a := [ 1 2 3 4 5 ]
a[1] := 9;
a
a[1] := 9
[ ... |
http://rosettacode.org/wiki/Zebra_puzzle | Zebra puzzle | Zebra puzzle
You are encouraged to solve this task according to the task description, using any language you may know.
The Zebra puzzle, a.k.a. Einstein's Riddle,
is a logic puzzle which is to be solved programmatically.
It has several variants, one of them this:
There are five houses.
The English man lives ... | #zkl | zkl | var people,drinks,houses,smokes,pets; // lists treated as associated arrays
fcn c2 { people.find(English)==houses.find(Red) }
fcn c3 { people.find(Swede)==pets.find(Dog) }
fcn c4 { people.find(Dane)==drinks.find(Tea) }
fcn c5 { (houses.find(Green) + 1)==houses.find(White) }
fcn c5a{ houses.find(Green)!=4 } // deduced c... |
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... | #OCaml | OCaml | let fix f g = (fun x a -> f (x x) a) (fun x a -> f (x x) a) 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... | #OCaml | OCaml | let zigzag n =
(* move takes references and modifies them directly *)
let move i j =
if !j < n - 1 then begin
i := max 0 (!i - 1);
incr j
end else
incr i
in
let a = Array.make_matrix n n 0
and x = ref 0 and y = ref 0 in
for v = 0 to n * n - 1 do
a.(!x).(!y) <- v;
if (!x + !... |
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... | #FBSL | FBSL | #AppType Console
DIM doors[], n AS INTEGER = 100
FOR DIM i = 1 TO n
FOR DIM j = i TO n STEP i
doors[j] = NOT doors[j]
NEXT
NEXT
FOR i = 1 TO n
IF doors[i] THEN PRINT "Door ", i, " is open"
NEXT
Pause |
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,... | #Mathematica_.2F_Wolfram_Language | Mathematica / Wolfram Language | a = Array[Sin, 10]
a[[1]]
Delete[a, 2] |
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... | #Oforth | Oforth | : Y(f) #[ f Y f perform ] ; |
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... | #Octave | Octave | function a = zigzag1(n)
j = 1:n;
u = repmat([-1; 1], n, 1);
v = j.*(2*j-3);
v = reshape([v; v+1], 2*n, 1);
a = zeros(n, n);
for i = 1:n
a(:, i) = v(i+j);
v += u;
endfor
endfunction
function a = zigzag2(n)
a = zigzag1(n);
v = (1:n-1)'.^2;
for i = 2:n
a(n+2-i:n, i) -= v(1:i-1);
endfor
... |
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... | #Fhidwfe | Fhidwfe |
doors = malloc$ 100u
for uint [0u, sizeof$ doors) with l1 {
put_byte$ + doors l1 as false byte
}
function void pass(step:uint) {
location = step
while <= location sizeof$ doors {
ac = - + doors location 1u
put_byte$ ac ~ deref_byte$ ac// true is represented as 255 (0xff)
location = + location step
... |
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,... | #MATLAB_.2F_Octave | MATLAB / Octave | >> a = [1 2 35] %Declaring a vector (i.e. one-dimensional array)
a =
1 2 35
>> a = [1 2 35;5 7 9] % Declaring a matrix (i.e. two-dimensional array)
a =
1 2 35
5 7 9
>> a3 = reshape(1:2*3*4,[2,3,4]); % declaring a three-dimensional array of size 2x3x4
a3 =
ans(:,:,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... | #Order | Order | #include <order/interpreter.h>
#define ORDER_PP_DEF_8y \
ORDER_PP_FN(8fn(8F, \
8let((8R, 8fn(8G, \
8ap(8F, 8fn(8A, 8ap(8ap(8G, 8G), 8A))))), \
... |
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... | #Inspired_by_Rascal | Inspired by Rascal |
#{
Produce a zigzag matrix. Nigel Galloway, January 26th., 2020.
At the time of writing the Rascal solution is yellow flagged for producing a striped matrix.
Let me make the same faux pas.
#}
n=5; g=1;
for e=1:n i=1; for l=e:-1:1 zig(i++,l)=g++; endfor endfor
for e=2:n i=e; for l=n:-1:e zig(i++,l)=g++; endfor en... |
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... | #Fish | Fish | 1001-p01.
>0101-p02.
>101-g001-g+:::aa*)?v101-p03.
>02-g?v1}02-p02. >05.
>0}02-p02.
>~~~0101-p001-g:1+001-paa*)?v02.
>07.
>0101-p08.
>101-g::02-g?v >1+:101-paa*=?;
>n" "o^ |
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,... | #Maxima | Maxima | /* Declare an array, subscripts run from 0 to max value */
array(a, flonum, 20, 20, 3)$
arrayinfo(a);
/* [complete, 3, [20, 20, 3]] */
a[0, 0]: 1.0;
listarray(a);
/* [1.0, 0.0, 0.0, ..., 0.0] */
/* Show all declared arrays */
arrays;
/* [a] */
/* One may also use an array without declaring it, it's a hashed... |
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... | #Oz | Oz | declare
Y = fun {$ F}
{fun {$ X} {X X} end
fun {$ X} {F fun {$ Z} {{X X} Z} end} end}
end
Fac = {Y fun {$ F}
fun {$ N}
if N == 0 then 1 else N*{F N-1} end
end
end}
Fib = {Y fun {$ F}
fun {$ N}
case ... |
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... | #ooRexx | ooRexx |
call printArray zigzag(3)
say
call printArray zigzag(4)
say
call printArray zigzag(5)
::routine zigzag
use strict arg size
data = .array~new(size, size)
row = 1
col = 1
loop element = 0 to (size * size) - 1
data[row, col] = element
-- even stripes
if (row + col) // 2 = 0 then do
... |
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... | #FOCAL | FOCAL | 1.1 F N=1,100;S D(N)=0
1.2 F M=1,100;F N=M,M,100;S D(N)=1-D(N)
1.3 F N=1,100;D 2.0
1.4 Q
2.1 I (D(N)),,2.2;R
2.2 T "OPEN DOOR ",%3.0,N,! |
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,... | #Mercury | Mercury | :- module array_example.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module array, int.
:- use_module exception.
:- type example_error ---> impossible.
main(!IO) :-
some [!A] ( % needed to introduce a state variable not present in the head
% Creat... |
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... | #PARI.2FGP | PARI/GP | Y(f)=x->f(f,x);
fact=Y((f,n)->if(n,n*f(f,n-1),1));
fib=Y((f,n)->if(n>1,f(f,n-1)+f(f,n-2),n));
apply(fact, [1..10])
apply(fib, [1..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... | #Oz | Oz | declare
%% state move success failure
States = unit(right: [ 1# 0 downLeft downInstead]
downInstead: [ 0# 1 downLeft terminate]
downLeft: [~1# 1 downLeft down]
down: [ 0# 1 topRight rightInstead]
... |
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... | #Forth | Forth | : toggle ( c-addr -- ) \ toggle the byte at c-addr
dup c@ 1 xor swap c! ;
100 1+ ( 1-based indexing ) constant ndoors
create doors ndoors allot
: init ( -- ) doors ndoors erase ; \ close all doors
: pass ( n -- ) \ toggle every nth door
ndoors over do
doors i + toggle
dup ( n ) +loop dro... |
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,... | #MiniScript | MiniScript | + list concatenation
* replication (i.e. repeat the list some number of times)
/ division (get some fraction of a list)
==, != comparison (for equality)
[i] get/set item i (first item is 0)
[i:j] get sublist ("slice") from i up to j
|
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... | #Perl | Perl | sub Y { my $f = shift; # λf.
sub { my $x = shift; $x->($x) }->( # (λx.x x)
sub {my $y = shift; $f->(sub {$y->($y)(@_)})} # λy.f λz.y y z
)
}
my $fac = sub {my $f = shift;
sub {my $n = shift; $n < 2 ? 1 : $n * $f->($n-1)}
};
my $fib = sub {my $f = shift;
... |
Subsets and Splits
Rosetta Code COBOL Python Hard Tasks
Identifies and retrieves challenging tasks that exist in both COBOL and Python, revealing cross-language programming patterns and difficulty levels for comparative analysis.
Rosetta Code Task Comparisons
Identifies tasks common to both COBOL and Python languages that are described as having difficulty levels, revealing cross-language task similarities and providing useful comparative programming examples.
Select Specific Languages Codes
Retrieves specific programming language names and codes from training data, providing basic filtering but limited analytical value beyond identifying these particular languages.