Because the best way to learn a language is copy-paste.
Prints Fibonacci numbers less than ten thousand:
fib b = 1 comefrom fib a next_b = a + b a = b b = next_b comefrom fib if a > 10000
The classic recursion example, that looks like recursion in Cf0x10, but really isn’t:
n = 5 # calculates n! acc = 1 factorial comefrom comefrom accumulate if n < 1 accumulate comefrom factorial acc = acc * n comefrom factorial if n is 0 n = n - 1 acc # prints the result
fizzbuzz mod_three = 3 mod_five = 5 comefrom fizzbuzz n comefrom fizzbuzz if n is mod_three comefrom fizzbuzz if n is mod_five n = n + 1 fizz comefrom fizzbuzz if n is mod_three 'Fizz'... mod_three = mod_three + 3 linebreak # would like to write "unless mod_three is mod_five" comefrom fizz if mod_three - mod_five - 3 '' buzz comefrom fizzbuzz if n is mod_five 'Buzz' mod_five = mod_five + 5 comefrom fizzbuzz if n is 100
99 bottles of beer¶
Using lyrics from http://99-bottles-of-beer.net/
bottles = ' bottles ' remaining = 99 one_less_bottle = remaining depluralize comefrom drinking if one_less_bottle is 1 bottles = ' bottle ' drinking comefrom if remaining is one_less_bottle remaining bottles 'of beer on the wall, ' remaining bottles 'of beer.' 'Take one down and pass it around, '... one_less_bottle = remaining - 1 one_less_bottle bottles 'of beer on the wall.`n' remaining = remaining - 1 comefrom if one_less_bottle is 0 'no more bottles of beer on the wall.' '' 'No more bottles of beer on the wall, no more bottles of beer.' 'Go to the store and buy some more, 99 bottles of beer on the wall.'
Just in case anyone doubted that Cf0x10 is Turing complete
# A Brainfuck interpreter in Comefrom0x10 # # Uses a Unicode string as storage, so behavior is undefined if cell value goes lower than zero # or higher than 0x10ffff. # # Storage is unbounded on both sides of the pointer. # # Invoke thus: # # cf0x10 brainfuck.cf0x10 bfprogram.b # pointer_alpha = 1/0 pointer_numeric = 1/0 tape_behind = '' tape_ahead = 1/0 tape_pos = 0 # only for debugging array_behind = 1/0 array_ahead = '' set_tape_ahead = array_ahead array_ahead = 1/0 # shift comefrom if array_ahead is array_ahead cdr = 1/0 cdr = array_ahead shift_tail = cdr new_cell comefrom shift if shift_tail is '' itoa = 0 shift_tail = itoa car = 1/0 car = array_ahead array_behind = car array_behind done = shift_tail array_ahead = shift_tail comefrom shift if array_ahead is done set_pointer_alpha = 1/0 set_pointer_alpha comefrom if set_pointer_alpha atoi = set_pointer_alpha cdr = tape_ahead set_tape_ahead = set_pointer_alpha cdr set_pointer_alpha = 1/0 set_tape_ahead = 1/0 set_pointer_vals comefrom if set_tape_ahead tape_ahead = set_tape_ahead car = tape_ahead pointer_alpha = car atoi = pointer_alpha pointer_numeric = atoi set_tape_ahead = 1/0 pointer_change = 1/0 change_pointer_val comefrom if pointer_change car = tape_ahead cdr = tape_ahead itoa = pointer_numeric + pointer_change set_tape_ahead = itoa cdr pointer_change = 1/0 file = 0 # initialize to something other than undefined so jump from file works when read fails read_path = argv error_reading_program comefrom file if file + 0 is 0 'Error: cannot read Brainfuck program at "' read_path '"' '' program_loaded comefrom file if file is file program_behind = '' program_ahead = file run comefrom program_loaded opcode = 1/0 opcode_numeric = 1/0 in_buffer = '' # cf0x10 stdin is line-buffered jumping = 0 moving = 1 comefrom run comefrom execute if opcode_numeric is 0 '' execute comefrom run if moving # can be useful for debugging: #program_ahead moving ':' jumping '@' tape_pos ':' pointer_numeric car = program_ahead atoi = car opcode_numeric = atoi opcode = car opcode = 1/0 # program_forward comefrom execute if moving > 0 array_behind = program_behind array_ahead = 1/0 array_ahead = program_ahead program_behind = array_behind program_ahead = array_ahead forward_jump comefrom execute if opcode is '[' jump comefrom forward_jump if pointer_numeric is 0 jumping = jumping + 1 moving = 1 match_brace comefrom forward_jump if jumping < 0 jumping = jumping + 1 stop_jump comefrom match_brace if jumping is 0 moving = 1 program_backward comefrom execute if moving < 0 array_behind = program_ahead array_ahead = 1/0 array_ahead = program_behind program_behind = array_ahead program_ahead = array_behind backward_jump comefrom execute if opcode is ']' jump comefrom backward_jump if pointer_numeric > 0 jumping = jumping - 1 moving = -1 match_brace comefrom backward_jump if jumping > 0 jumping = jumping - 1 stop_jump comefrom match_brace if jumping is 0 moving = 1 op comefrom execute if opcode moving = 1 do_op = opcode comefrom op if jumping # forward comefrom op if do_op is '>' tape_pos = tape_pos + 1 array_ahead = 1/0 array_behind = tape_behind array_ahead = tape_ahead tape_behind = array_behind set_tape_ahead = array_ahead backward comefrom op if do_op is '<' tape_pos = tape_pos - 1 array_ahead = 1/0 array_behind = tape_ahead array_ahead = tape_behind tape_behind = array_ahead set_tape_ahead = array_behind increment comefrom op if do_op is '+' pointer_change = 1 decrement comefrom op if do_op is '-' pointer_change = -1 print comefrom op if do_op is '.' pointer_alpha... read comefrom op if do_op is ',' # cdr = 1/0 cdr = in_buffer car = in_buffer set_pointer_alpha = car cdr = in_buffer in_buffer = cdr comefrom stdin if stdin + 0 is 0 # block_for_input comefrom read if cdr is '' stdin = '' in_buffer = stdin cdr = in_buffer comefrom stdin if stdin + 0 is 0