Skip to content

caioariede/rice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rice Programming Language

Currently, Rice is under heavy development and brainstorm stage. Not usable yet.

The objective is to maintain total interoperability between Rice and Erlang, and do not miss the functional paradigm.

  • Variables are immutable (single-assignment)
  • No imperative loops (for, while, etc)
  • No global scope

Three words in my mind

  • Powerful
  • Elegant
  • Clean

Data types

Strings

“Hello world” and ‘Hello world’ are same.

>>> "Hello world" === 'Hello world'
true
>>> "foo".str?
true

Integer

>>> 1.int?
true
>>> 1.float?
false

Float

>>> 1.0.float?
true
>>> (2 * 0.5).int?
false

Atoms

Erlang atoms, like Ruby symbols.

:keyword or :‘this is an atom’ are valid atoms.

>>> :keyword.atom?
true
>>> :'this is an atom'.atom?
true
>>> 1.atom?
false

Lists

>>> L = [1, 2, 3]
[1, 2, 3]
>>> L[1]
2

Like Python, slice notation is also supported:

>>> L = [2, 4, 6, 8]
[2, 4, 6, 8]
>>> L[0:2]
[2, 4]
>>> L[:3]
[2, 4, 6]
>>> L[-1:]
[8]

Special atoms

true, false and undefined are considered special atoms.

>>> true == :true
true
>>> false == :false
true
>>> undefined == :undefined
true

Just a syntatic sugar here.

Operators

Comparison operators

>>> true and true
true
>>> true or false
true
>>> not true
false
>>> not (not true)
true
>>> 1 == 1.0
true
>>> 1 === 1.0
false

Logical operators

>>> 1 & 1 # and
1
>>> 0 | 0 # or
0
>>> 1 ^ 1 # xor
0
>>> ~2 # negation
-3
>>> 6 >> 1 # shift right
4
>>> 6 << 4 # shift left
96

Function definition

def fib (0)
    0

def fib (1)
    1

def fib (n) when n > 0
    fib(n - 1) + fib(n - 2)

end

Build-in functions for each data type (not OOP)

>>> 1.to_s
"1"
>>> 1.puts
1
>>> "hello world".puts
"hello world"
>>> [1, 2, 3, 4].filter(fun x: x.even?)
[2, 4]
>>> [1, 2, 3, 4].filter(fun x: x.even? and x < 4)
[2]
>>> foo = undefined
>>> foo.to_s
"undefined"

Anonymous functions

>>> my_function = fun x
        x + 1
    end

>>> my_function(2)
3

Anonymous functions with multiple clauses

>>> test = fun 1
        true
    fun 0
        false
    end

>>> test(1)
true
>>> test(0)
false

If-then-else

x = 1
#x = "foobar"
#x = 1.0

if x.int?
    puts "integer"
elif x.str?
    puts "string"
else
    puts "anything else"
end

Decorators

def debug fn
    fun x, y
        puts "called function %s" % fn.name
        fn.call x, y
    end
end

@debug
def sum x, y
    x + y
end

List comprehension

Example of permutation using two generators

>>> [x + y for x in "ab", y in "ab"]
["aa", "ab", "ba", "bb"]

Partial application

>>> double = 2 * ?
>>> double(2)
3
>>> cube = ?1 * ?1 * ?1
>>> cube(3)
27
>>> [10, 20, 30].map(2 * ?)
[20, 40, 60]

Predicate functions

All functions with name terminating in “?” are evaluated to true or false

>>> def foo?
        "bar"
    end

>>> foo?
true
>>> def bar?
        0
    end

>>> bar?
false

About

A functional programming language based on Python and Ruby that runs on the Erlang VM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages