Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

ITT: Language Design

Name: Anonymous 2010-11-15 22:22

If you had the time and will to create a new Language, what would it look like?

My attempt at a functional, yet object oriented language, without the retarded syntax of haskell and without the forced intendation of python:


import IO
import OS
import Text

// braces are only required when a function
// contains more than one statement
def printFmt $format ... = do
    IO.WriteStr OS.Stdout (Text.Format $format ...)


// you can define an explicit return type by appending
// ':<TYPE>' to the end of the argument list
def fib $x :int = do
{
    if $x > 3 then
    {
        return (fib (x - 1)) + (fib ($x - 2))
    }
    return 1
}

// just like in python, all class members are public
class App {
    var $name

    def App = do
        // the 'nothing' keyword works like the 'pass' keyword -
        // it does efficiently nothing, and immediately returns
        // the default value of the specified returntype.
        // example: if the function is returned as 'def foo :int',
        // the returned value is 0.
        nothing


    def setName $n = do
        // extra dollarsign is not needed when refering to
        // class members.
        // to add explicity, members have to be called through '$this'
        $this.name = $n

    def sayName = do
        printLn "name is {0}" $this.name
}

/*
    'var $num = 15' could be also written as

        def $num = 15

    since the last argument is returned on a function without
    type specifier.
*/
var $num = 15
printLn "fib({0}) = {1}\n" $num (fib $num)

Name: Anonymous 2010-11-16 1:57

My language will be like this.

http://rosettacode.org/wiki/Function_composition


fun compose(f : Lambda[$Out, $Int], g : Lambda[Int, $In])=
   MakeLambda[Out, In]{return f(g($x))}


expression $x in lambda body($x) uses to introduce argument x of type In(in Lambdas first template argument is output parameter, rest - input). In Lambda[$Out, $Int] it uses to introduce type. Code above is equivalent to


fun [Out, Int, In] //Template function arguments
compose(f : Lambda[Out, Int], g : Lambda[Int, In]) : Lambda[Out,In]
   return Lambda[Out, In]{var $x : In; return f(g(x))}


here dollar sign in variable declaration $x : In tells compiler to use x as argument.

notice = after function declaration.
fun name(args) = expr is used to declare function with type = typeof(expr). in this case it's lambda.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

http://rosettacode.org/wiki/Rot-13



fun rot13(in:String)
  rvar out = in.map {
     var base = match $x
        | 'A'..'Z': 'A'.ord
        | 'a'..'z': 'a'.ord
        | else: return $x
     var idx = ($x.ord - base + 13) % 26
     return (base+idx).chr
  }


rvar is shorthand for "return value variable". Function will have return type of variable out (String in this case).


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ADT and non-context free grammar. Because of DRY grammar is not context free. Examples


adt Color
 | Red
 | Blue
 | Green

var color0 = Color.Red
var color1 : Color = Red //we know that color1 is Color type, so compiler can tell that Red is actually Color.Red here.

// || is string concatenation
if color == Red //color is Color, therefore Red is Color.Red
    printf "Color: " || Red //compiler error
    printf "Color: " || Color.Red //ok



-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Recursive types


fun callbackChainMember( arg : Int, nextCallback : Func.Self ):Int
      return nextCallback(arg+1, NULL)

Func.Self is the type Lambda[/*ret*/Int, /*arg*/Int, /*that lambda itself*/]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List