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/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#Symsyn
Symsyn
  | FizzBuzz   1 I if I LE 100 mod I 3 X mod I 5 Y if X EQ 0 'FIZZ' $S if Y EQ 0 + 'BUZZ' $S endif else if Y EQ 0 'BUZZ' $S else ~ I $S endif endif $S [] + I goif endif  
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#Forth
Forth
MODULE BRAIN !It will suffer. INTEGER MSG,KBD CONTAINS !A twisted interpreter. SUBROUTINE RUN(PROG,STORE) !Code and data are separate! CHARACTER*(*) PROG !So, this is the code. CHARACTER*(1) STORE(:) !And this a work area. CHARACTER*1 C !The code of the moment. ...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#FreeBASIC
FreeBASIC
' version 01-07-2018 ' compile with: fbc -s console   Randomize Timer Const As UInteger children = 100 Const As Double mutate_rate = 0.05   Function fitness(target As String, tmp As String) As UInteger   Dim As UInteger x, f   For x = 0 To Len(tmp) -1 If tmp[x] = target[x] Then f += 1 Next Retur...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#MATLAB
MATLAB
function f = fib(n)   f = [1 1 ; 1 0]^(n-1); f = f(1,1);   end
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#R
R
  e <- simpleError("This is a simpleError")  
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Racket
Racket
  #lang racket   ;; define a new exception type (struct exn:my-exception exn ())   ;; handler that prints the message ("Hi!") (define (handler exn) (displayln (exn-message exn)))   ;; install exception handlers (with-handlers ([exn:my-exception? handler])  ;; raise the exception (raise (exn:my-exception "Hi!" (cur...
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#NewLISP
NewLISP
(exec "ls")
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Nim
Nim
import osproc   let exitCode = execCmd "ls" let (output, exitCode2) = execCmdEx "ls"
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Objective-C
Objective-C
void runls() { [[NSTask launchedTaskWithLaunchPath:@"/bin/ls" arguments:@[]] waitUntilExit]; }
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#Brainf.2A.2A.2A
Brainf***
>++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>[[-]>[<<+>+>-]<[>+<-]<[>+<-[>+<-[> +<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>>>>+>+<<<<<<-[>+<-]]]]]]]]]]]>[<+>- ]+>>>>>]<<<<<[<<<<<]>>>>>>>[>>>>>]++[-<<<<<]>>>>>>-]+>>>>>]<[>++<-]<<<<[<[ >+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>>]<<<<<[<[>+>+<<-]>.<<<<<]>.>>>>]
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Scheme
Scheme
(define (^ base exponent) (define (*^ exponent acc) (if (= exponent 0) acc (*^ (- exponent 1) (* acc base)))) (*^ exponent 1))   (display (^ 2 3)) (newline) (display (^ (/ 1 2) 3)) (newline) (display (^ 0.5 3)) (newline) (display (^ 2+i 3)) (newline)
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#Tailspin
Tailspin
  templates fizz $ mod 3 -> # when <=0> do 'Fizz' ! end fizz   templates buzz $ mod 5 -> # when <=0> do 'Buzz' ! end buzz   [ 1..100 -> '$->fizz;$->buzz;' ] -> \[i](when <=''> do $i ! otherwise $ !\)... -> '$; ' -> !OUT::write  
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#Fortran
Fortran
MODULE BRAIN !It will suffer. INTEGER MSG,KBD CONTAINS !A twisted interpreter. SUBROUTINE RUN(PROG,STORE) !Code and data are separate! CHARACTER*(*) PROG !So, this is the code. CHARACTER*(1) STORE(:) !And this a work area. CHARACTER*1 C !The code of the moment. ...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#F.C5.8Drmul.C3.A6
Fōrmulæ
package main   import ( "fmt" "math/rand" "time" )   var target = []byte("METHINKS IT IS LIKE A WEASEL") var set = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ ") var parent []byte   func init() { rand.Seed(time.Now().UnixNano()) parent = make([]byte, len(target)) for i := range parent { parent[i]...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#Maxima
Maxima
/* fib(n) is built-in; here is an implementation */ fib2(n) := (matrix([0, 1], [1, 1])^^n)[1, 2]$   fib2(100)-fib(100); 0   fib2(-10); -55
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Raku
Raku
try { die "Help I'm dieing!"; CATCH { when X::AdHoc { note .Str.uc; say "Cough, Cough, Aiee!!" } default { note "Unexpected exception, $_!" } } }   say "Yay. I'm alive.";   die "I'm dead.";   say "Arrgh.";   CATCH { default { note "No you're not."; say $_.Str; } }
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Raven
Raven
42 as custom_error   define foo custom_error throw   try foo catch custom_error = if 'oops' print
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#OCaml
OCaml
Sys.command "ls"
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Octave
Octave
system("ls");
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#Brat
Brat
factorial = { x | true? x == 0 1 { x * factorial(x - 1)} }
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Seed7
Seed7
const func integer: intPow (in var integer: base, in var integer: exponent) is func result var integer: result is 0; begin if exponent < 0 then raise(NUMERIC_ERROR); else if odd(exponent) then result := base; else result := 1; end if; exponent := exponent di...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Sidef
Sidef
func expon(_, {.is_zero}) { 1 }   func expon(base, exp {.is_neg}) { expon(1/base, -exp) }   func expon(base, exp {.is_int}) {   var c = 1 while (exp > 1) { c *= base if exp.is_odd base *= base exp >>= 1 }   return (base * c) }   say expon(3, 10) say expon(5.5, -3)
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#Tcl
Tcl
proc fizzbuzz {n {m1 3} {m2 5}} { for {set i 1} {$i <= $n} {incr i} { set ans "" if {$i % $m1 == 0} {append ans Fizz} if {$i % $m2 == 0} {append ans Buzz} puts [expr {$ans eq "" ? $i : $ans}] } } fizzbuzz 100
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#FreeBASIC
FreeBASIC
  ' Intérprete de brainfuck ' FB 1.05.0 Win64 '   Const BF_error_memoria_saturada As Integer = 2 Const BF_error_memoria_insuficiente As Integer = 4 Const BF_error_codigo_saturado As Integer = 8 Const BF_error_desbordamiento_codigo As Integer = 16   Dim BFcodigo As String = ">++++++++++[>+++>+++++++>++++++++++>+++++++++...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#Go
Go
package main   import ( "fmt" "math/rand" "time" )   var target = []byte("METHINKS IT IS LIKE A WEASEL") var set = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ ") var parent []byte   func init() { rand.Seed(time.Now().UnixNano()) parent = make([]byte, len(target)) for i := range parent { parent[i]...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#MAXScript
MAXScript
fn fibIter n = ( if n < 2 then ( n ) else ( fib = 1 fibPrev = 1 for num in 3 to n do ( temp = fib fib += fibPrev fibPrev = temp ) fib ) )
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#REXX
REXX
/*REXX program demonstrates handling an exception (negative #); catching is via a label.*/ do j=9 by -5 say 'the square root of ' j " is " sqrt(j) end /*j*/ exit /*stick a fork in it, we're all done. */ /*──────────────────...
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Ring
Ring
  Try see 1/0 Catch raise("Sorry we can't divide 1/0 + nl) Done  
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Oforth
Oforth
System cmd("pause")
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Oz
Oz
{OS.system "ls" _}
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#PARI.2FGP
PARI/GP
system("ls")
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#Burlesque
Burlesque
  blsq ) 6?! 720  
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Slate
Slate
x@(Number traits) raisedTo: y@(Integer traits) [ y isZero ifTrue: [^ x unit]. x isZero \/ [y = 1] ifTrue: [^ x]. y isPositive ifTrue: "(x * x raisedTo: y // 2) * (x raisedTo: y \\ 2)" [| count result | count: 1. [(count: (count bitShift: 1)) < y] whileTrue. result: x unit. ...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Smalltalk
Smalltalk
Number extend [ ** anInt [ | r | ( anInt isInteger ) ifFalse: [ '** works fine only for integer powers' displayOn: stderr . Character nl displayOn: stderr ]. r := 1. 1 to: anInt do: [ :i | r := ( r * self ) ]. ^r ] ].   ( 2.5 ** 3 ) displayNl. ( ...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#TI-83_BASIC
TI-83 BASIC
: FIZZBUZZ 101 1 DO I 15 MOD 0 = IF PRINT " FIZZBUZZ " ELSE I 3 MOD 0 = IF PRINT " FIZZ " ELSE I 5 MOD 0 = IF PRINT " BUZZ " ELSE I . THEN THEN THEN CR LOOP ;
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#Furor
Furor
  argc 3 < { ."Usage: furor brainfuck.upu brainfuckpgmfile\n" }{ 2 argv getfile // dup #s print free sto bfpgm 100000 mem dup maximize sto bfmem // Memóriaallokáció a brainfuck memóriaterület számára tick sto startingtick sbr §brainfuck NL tick @startingtick #g - ."Time = " print ." tick\n" @bfmem free // A lefoglalt m...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#Haskell
Haskell
import System.Random import Control.Monad import Data.List import Data.Ord import Data.Array   showNum :: (Num a, Show a) => Int -> a -> String showNum w = until ((>w-1).length) (' ':) . show   replace :: Int -> a -> [a] -> [a] replace n c ls = take (n-1) ls ++ [c] ++ drop n ls   target = "METHINKS IT IS LIKE A WEASEL...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#Mercury
Mercury
  % The following code is derived from the Mercury Tutorial by Ralph Becket. % http://www.mercury.csse.unimelb.edu.au/information/papers/book.pdf :- module fib.   :- interface. :- import_module io. :- pred main(io::di, io::uo) is det.   :- implementation. :- import_module int.   :- pred fib(int::in, int::out) is det. f...
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Ruby
Ruby
# define an exception class SillyError < Exception end
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Rust
Rust
// IO error is used here just as an example of an already existing // Error use std::io::{Error, ErrorKind};   // Rust technically doesn't have exception, but different // types of error handling. Here are two examples of results.   fn valid_function() -> Result<usize, Error> { Ok(100) }   fn errored_function() -> ...
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Pascal
Pascal
Program ExecuteSystemCommand;   uses SysUtils; begin ExecuteProcess('/bin/ls', '-alh'); end.
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#PDP-11_Assembly
PDP-11 Assembly
; Execute a file - the equivalent of system() in stdio ; ; On entry, r1=>nul-terminated command string ; On exit, VS=Couldn't fork ; VC=Forked successfully, r0=return value ; .CLIsystem trap 2 ; fork() br CLIchild ; Child process returns here bcc CLIparent ; Parent process returns here mov (sp)+,r1 t...
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#C
C
int factorial(int n) { int result = 1; for (int i = 1; i <= n; ++i) result *= i; return result; }  
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Standard_ML
Standard ML
fun expt_int (a, b) = let fun aux (x, i) = if i = b then x else aux (x * a, i + 1) in aux (1, 0) end   fun expt_real (a, b) = let fun aux (x, i) = if i = b then x else aux (x * a, i + 1) in aux (1.0, 0) end   val op ** = expt_int infix 6 ** val op *** = expt_real infix 6 ***
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Stata
Stata
mata function pow(a, n) { x = a for(p=1; n>0; n=floor(n/2)) { if(mod(n,2)==1) p = p*x x = x*x } return(p) } end
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#TI-83_Hex_Assembly
TI-83 Hex Assembly
: FIZZBUZZ 101 1 DO I 15 MOD 0 = IF PRINT " FIZZBUZZ " ELSE I 3 MOD 0 = IF PRINT " FIZZ " ELSE I 5 MOD 0 = IF PRINT " BUZZ " ELSE I . THEN THEN THEN CR LOOP ;
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#GAP
GAP
# Here . and , print and read an integer, not a character Brainfuck := function(prog) local pointer, stack, leftcells, rightcells, instr, stackptr, len, output, input, jump, i, j, set, get; input := InputTextUser(); output := OutputTextUser(); instr := 1; pointer := 0; leftcells := [ ]; rightcells := ...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#Icon_and_Unicon
Icon and Unicon
global target, chars, parent, C, M, current_fitness   procedure fitness(s) fit := 0 #Increment the fitness for every position in the string s that matches the target every i := 1 to *target & s[i] == target[i] do fit +:= 1 return fit end   procedure mutate(s) #If a random number between 0 and 1 is inside the bound...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#Metafont
Metafont
vardef fibo(expr n) = if n=0: 0 elseif n=1: 1 else: fibo(n-1) + fibo(n-2) fi enddef;   for i=0 upto 10: show fibo(i); endfor end
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Scala
Scala
//Defining exceptions class AccountBlockException extends Exception class InsufficientFundsException(val amount: Double) extends Exception   class CheckingAccount(number: Int, var blocked: Boolean = false, var balance: Double = 0.0) { def deposit(amount: Double) { // Throwing an exception 1 if (blocked) throw new...
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Perl
Perl
my @results = qx(ls); # run command and return STDOUT as a string   my @results = `ls`; # same, alternative syntax   system "ls"; # run command and return exit status; STDOUT of command goes program STDOUT   print `ls`; # same, but with back quotes   exec "ls"; # replace current pro...
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Phix
Phix
without js string cmd = iff(platform()=WINDOWS?"dir":"ls") system(cmd) integer res = system_exec("pause",4)
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#C.23
C#
using System;   class Program { static int Factorial(int number) { if(number < 0) throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");   var accumulator = 1; for (var factor = 1; factor <= number; factor++) { ...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Swift
Swift
func raise<T: Numeric>(_ base: T, to exponent: Int) -> T { precondition(exponent >= 0, "Exponent has to be nonnegative") return Array(repeating: base, count: exponent).reduce(1, *) }   infix operator **: MultiplicationPrecedence   func **<T: Numeric>(lhs: T, rhs: Int) -> T { return raise(lhs, to: rhs) }   l...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Tcl
Tcl
package require Tcl 8.5 proc tcl::mathfunc::mypow {a b} { if { ! [string is int -strict $b]} {error "exponent must be an integer"} set res 1 for {set i 1} {$i <= $b} {incr i} {set res [expr {$res * $a}]} return $res } expr {mypow(3, 3)} ;# ==> 27 expr {mypow(3.5, 3)} ;# ==> 42.875 expr {mypow(3.5, 3.2)}...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#TransFORTH
TransFORTH
: FIZZBUZZ 101 1 DO I 15 MOD 0 = IF PRINT " FIZZBUZZ " ELSE I 3 MOD 0 = IF PRINT " FIZZ " ELSE I 5 MOD 0 = IF PRINT " BUZZ " ELSE I . THEN THEN THEN CR LOOP ;
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#Go
Go
package main   import "fmt"   func main() { // example program is current Brain**** solution to // Hello world/Text task. only requires 10 bytes of data store! bf(10, `++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++ ++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>> >+..<.<++++++++.>>>+.<<+.<<<<+++...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#J
J
CHARSET=: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' NPROG=: 100 NB. number of progeny (C) MRATE=: 0.05 NB. mutation rate   create =: (?@$&$ { ])&CHARSET NB. creates random list from charset of same shape as y fitness =: +/@:~:"1 copy =: # ,: mutate =: &(>: $ ?...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#Microsoft_Small_Basic
Microsoft Small Basic
' Fibonacci sequence - 31/07/2018 n = 139 f1 = 0 f2 = 1 TextWindow.WriteLine("fibo(0)="+f1) TextWindow.WriteLine("fibo(1)="+f2) For i = 2 To n f3 = f1 + f2 TextWindow.WriteLine("fibo("+i+")="+f3) f1 = f2 f2 = f3 EndFor
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Scheme
Scheme
(define (me-errors xx exception) (if (even? xx) xx (exception)))   ;example that does nothing special on exception (call/cc (lambda (exception) (me-errors 222 exception) (display "I guess everything is alright")))   ;example that laments oddness on exception (call/cc (lambda (all-ok) ;used t...
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Seed7
Seed7
const proc: foo is func begin raise RANGE_ERROR; end func;
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#PHP
PHP
@exec($command,$output); echo nl2br($output);
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#PicoLisp
PicoLisp
(call "ls")
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#C.2B.2B
C++
#include <boost/iterator/counting_iterator.hpp> #include <algorithm>   int factorial(int n) { // last is one-past-end return std::accumulate(boost::counting_iterator<int>(1), boost::counting_iterator<int>(n+1), 1, std::multiplies<int>()); }
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Ursa
Ursa
# these implementations ignore negative exponents def intpow (int m, int n) if (< n 1) return 1 end if decl int ret set ret 1 for () (> n 0) (dec n) set ret (* ret m) end for return ret end intpow   def floatpow (double m, int n) if (or (< n 1) (and (= m 0) (= n 0))) return 1 end if decl int ret set re...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#VBA
VBA
Public Function exp(ByVal base As Variant, ByVal exponent As Long) As Variant Dim result As Variant If TypeName(base) = "Integer" Or TypeName(base) = "Long" Then 'integer exponentiation result = 1 If exponent < 0 Then result = IIf(Abs(base) <> 1, CVErr(2019), IIf(exponent Mod...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#True_BASIC
True BASIC
for i : 1 .. 100 if i mod 15 = 0 then put "Fizzbuzz" elsif i mod 5 = 0 then put "Buzz" elsif i mod 3 = 0 then put "Fizz" else put i end if end for
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#Groovy
Groovy
class BrainfuckProgram {   def program = '', memory = [:] def instructionPointer = 0, dataPointer = 0   def execute() { while (instructionPointer < program.size()) switch(program[instructionPointer++]) { case '>': dataPointer++; break; case '<': dataPointer--; bre...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#Java
Java
  import java.util.Random;   public class EvoAlgo { static final String target = "METHINKS IT IS LIKE A WEASEL"; static final char[] possibilities = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ".toCharArray(); static int C = 100; //number of spawn per generation static double minMutateRate = 0.09; static int perfectFitness =...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#min
min
( (2 <) ((0 1 (dup rollup +)) dip pred times nip) unless ) :fib
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Sidef
Sidef
try { die "I'm dead!"; # throws an exception of type 'error' } catch { |type, msg| say "type: #{type}"; # type: error say "msg: #{msg}"; # msg: I'm dead! at test.sf line 2. };   say "I'm alive..."; die "Now I'm dead!"; # this line terminates the program say "Or am I?"; # Y...
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Slate
Slate
se@(SceneElement traits) doWithRestart: block [ block handlingCases: {Abort -> [| :_ | ^ Nil]} ].
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Smalltalk
Smalltalk
"exec" "gst" "-f" "$0" "$0" "$*" "exit"   Transcript show: 'Throwing yawp'; cr. self error: 'Yawp!'.
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Pike
Pike
int main(){ // Process.run was added in Pike 7.8 as a wrapper to simplify the use of Process.create_process() mapping response = Process.run("ls -l"); // response is now a map containing 3 fields // stderr, stdout, and exitcode. We want stdout. write(response["stdout"] + "\n");   // with older version...
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Pop11
Pop11
sysobey('ls');
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#C3
C3
fn int factorial(int n) { int result = 1; for (int i = 1; i <= n; ++i) { result *= i; } return result; }  
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#VBScript
VBScript
  Function pow(x,y) pow = 1 If y < 0 Then For i = 1 To Abs(y) pow = pow * (1/x) Next Else For i = 1 To y pow = pow * x Next End If End Function   WScript.StdOut.Write "2 ^ 0 = " & pow(2,0) WScript.StdOut.WriteLine WScript.StdOut.Write "7 ^ 6 = " & pow(7,6) WScript.StdOut.WriteLine WScript.StdOut.Writ...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#XPL0
XPL0
include c:\cxpl\codes; \intrinsic 'code' declarations   func real Power(X, Y); \X raised to the Y power; (X > 0.0) real X; int Y; return Exp(float(Y) * Ln(X));   func IPower(X, Y); \X raised to the Y power int X, Y; int P; [P:= 1; while Y do [if Y&1 then P:= P*X; X:= X*X; Y:= Y>>1; ]; return P...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#Turing
Turing
for i : 1 .. 100 if i mod 15 = 0 then put "Fizzbuzz" elsif i mod 5 = 0 then put "Buzz" elsif i mod 3 = 0 then put "Fizz" else put i end if end for
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#GW-BASIC
GW-BASIC
10 REM BRAINFK INTERPRETER FOR GW-BASIC 20 INPUT "File to open? ",INFILE$ 30 DIM TAPE(10000): REM memory is 10000 long 40 DIM PRG$(5000): REM programs can be 5000 symbols long 50 PRG$ = "" 60 OPEN(INFILE$) FOR INPUT AS #1 70 S = 0 : REM instruction pointer 80 WHILE NOT EOF(1) 90 LINE INPUT #1, LIN$ 100 FOR P = 1 TO ...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#JavaScript
JavaScript
// ------------------------------------- Cross-browser Compatibility -------------------------------------   /* Compatibility code to reduce an array * Source: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce */ if (!Array.prototype.reduce) { Array.prototype.reduce = function (fun...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#MiniScript
MiniScript
fibonacci = function(n) if n < 2 then return n n1 = 0 n2 = 1 for i in range(n-1, 1) ans = n1 + n2 n1 = n2 n2 = ans end for return ans end function   print fibonacci(6)
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#SQL_PL
SQL PL
  --#SET TERMINATOR @   BEGIN DECLARE numerator INTEGER DEFAULT 12; DECLARE denominator INTEGER DEFAULT 0; DECLARE RESULT INTEGER; DECLARE overflow CONDITION FOR SQLSTATE '22003' ; DECLARE CONTINUE HANDLER FOR overflow RESIGNAL SQLSTATE '22375' SET MESSAGE_TEXT = 'Zero division'; IF denominator = 0 THEN S...
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#PowerShell
PowerShell
dir ls Get-ChildItem
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Prolog
Prolog
shell('ls').
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#Cat
Cat
define rec_fac { dup 1 <= [pop 1] [dec rec_fac *] if }
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Wren
Wren
class Num2 { static ipow(i, exp) { if (!i.isInteger) Fiber.abort("ipow method must have an integer receiver") if (!exp.isInteger) Fiber.abort("ipow method must have an integer exponent") if (i == 1 || exp == 0) return 1 if (i == -1) return (exp%2 == 0) ? 1 : -1 if (exp < 0) F...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#TUSCRIPT
TUSCRIPT
$$ MODE TUSCRIPT LOOP n=1,100 mod=MOD (n,15) SELECT mod CASE 0 PRINT n," FizzBuzz" CASE 3,6,9,12 PRINT n," Fizz" CASE 5,10 PRINT n," Buzz" DEFAULT PRINT n ENDSELECT ENDLOOP
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#Haskell
Haskell
import java.io.IOException; public class Interpreter { public final static int MEMORY_SIZE = 65536; private final char[] memory = new char[MEMORY_SIZE]; private int dp; private int ip; private int border; private void reset() { for (int i = 0; i < MEMORY_SIZE; i++) { mem...
http://rosettacode.org/wiki/Evolutionary_algorithm
Evolutionary algorithm
Starting with: The target string: "METHINKS IT IS LIKE A WEASEL". An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent). A fitness function that computes the ‘closeness’ of its argument to the target string....
#Julia
Julia
fitness(a::AbstractString, b::AbstractString) = count(l == t for (l, t) in zip(a, b)) function mutate(str::AbstractString, rate::Float64) L = collect(Char, " ABCDEFGHIJKLMNOPQRSTUVWXYZ") return map(str) do c if rand() < rate rand(L) else c end end end   function evolve(parent::String, target::String...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#MiniZinc
MiniZinc
function var int: fibonacci(int: n) = let { array[0..n] of var int: fibonacci; constraint forall(a in 0..n)( fibonacci[a] = if (a == 0 \/ a == 1) then a else fibonacci[a-1]+fibonacci[a-2] endif ) } in fibonacci[n];   var int: fib = fibonacci(6); solve satisfy; output [s...
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Standard_ML
Standard ML
exception MyException; exception MyDataException of int; (* can be any first-class type, not just int *)
http://rosettacode.org/wiki/Exceptions
Exceptions
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops This task is to give an example of an exception handling routine and to "throw" a new exception. Related task   Exceptions Through Nested Calls
#Stata
Stata
capture confirm file titanium.dta if _rc { if _rc==601 { display "the file does not exist" } else { * all other cases display "there was an error with return code " _rc } }
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#PureBasic
PureBasic
ImportC "msvcrt.lib" system(str.p-ascii) EndImport   If OpenConsole() system("dir & pause")   Print(#CRLF$ + #CRLF$ + "Press ENTER to exit") Input() CloseConsole() EndIf
http://rosettacode.org/wiki/Execute_a_system_command
Execute a system command
Task Run either the   ls   system command   (dir   on Windows),   or the   pause   system command. Related task Get system command output
#Python
Python
import os exit_code = os.system('ls') # Just execute the command, return a success/fail code output = os.popen('ls').read() # If you want to get the output data. Deprecated.
http://rosettacode.org/wiki/Factorial
Factorial
Definitions   The factorial of   0   (zero)   is defined as being   1   (unity).   The   Factorial Function   of a positive integer,   n,   is defined as the product of the sequence: n,   n-1,   n-2,   ...   1 Task Write a function to return the factorial of a number. Solutions can be iterat...
#Ceylon
Ceylon
shared void run() {   Integer? recursiveFactorial(Integer n) => switch(n <=> 0) case(smaller) null case(equal) 1 case(larger) if(exists f = recursiveFactorial(n - 1)) then n * f else null;     Integer? iterativeFactorial(Integer n) => switch(n <=> 0) case(smaller) null case(equal) 1 case(larg...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#zkl
zkl
fcn pow(n,exp){ reg v; if(n.isType(1)){ // Int if (exp<0) return(if(n*n!=1) 0 else (if(exp.isOdd) n else 1)); v=1; }else{ if(exp<0){ n=1.0/n; exp=-exp; } v=1.0; } while(exp>0){ if(exp.isOdd) v*=n; n*=n; exp/=2; } v }
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#ZX_Spectrum_Basic
ZX Spectrum Basic
10 PRINT e(3,2): REM 3 ^ 2 20 PRINT e(1.5,2.7): REM 1.5 ^ 2.7 30 STOP 9950 DEF FN e(a,b)=a^b
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#TXR
TXR
$ txr -p "(mapcar (op if @1 @1 @2) (repeat '(nil nil fizz nil buzz fizz nil nil fizz buzz nil fizz nil nil fizzbuzz)) (range 1 100))"
http://rosettacode.org/wiki/Execute_Brain****
Execute Brain****
Execute Brain**** is an implementation of Brainf***. Other implementations of Brainf***. RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions:...
#Icon_and_Unicon
Icon and Unicon
import java.io.IOException; public class Interpreter { public final static int MEMORY_SIZE = 65536; private final char[] memory = new char[MEMORY_SIZE]; private int dp; private int ip; private int border; private void reset() { for (int i = 0; i < MEMORY_SIZE; i++) { mem...