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: Larry 2010-11-17 6:29

# Specification:
# P91 (**) Knight's tour
# Another famous problem is this one: How can a knight jump on an NxN
# chessboard in such a way that it visits every square exactly once?
#
# Hints: Represent the squares by pairs of their coordinates of the form
# X/Y, where both X and Y are integers between 1 and N. (Note that '/'
# is just a convenient functor, not division!) Define the relation
# jump(N,X/Y,U/V) to express the fact that a knight can jump from X/Y to
# U/V on a NxN chessboard. And finally, represent the solution of our
# problem as a list of N*N knight positions (the knight's tour).


my $n = 5;
my $size = $n * $n;

my @track;
my @directions = (1, -1 X 2, -2), (2, -2 X 1, -1);

sub valid_moves($curr, @temp_track=@track) {
  my @valid_squares = @directions.map(->$a,$b { ($curr.key + $a) => ($curr.value + $b) }).grep({0 <= all(.key, .value) < $n});
  # exclude occupied squares. !eqv doesn't work yet.
  @valid_squares.grep({ not $_ eqv any(@temp_track, $curr) });
}

sub knight($square) {
  @track.push($square);
  return 1 if @track.elems == $size;

  # simple heuristic, for move ordering
  my @possible_moves = valid_moves($square).sort: ->$a,$b {
    valid_moves($a, [@track,$a]).elems <=> valid_moves($b, [@track, $b]).elems;
  };

  return unless @possible_moves.elems;

  for @possible_moves -> $try {
    my $result = knight($try);
    if $result {
      return 1;
    } else {
      @track.pop;
    }
  }
}

if knight(0 => 0) {
  say "FOUND: " ~ @track.perl;
} else {
  say "NOT FOUND";
}

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