Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!

Musings on Epoch features

Posted by ApochPiQ, 24 April 2012 · 1,091 views

I'll just leave this here...

// Define a recursive structure for a singly linked list
// A list must contain at least one element. If empty lists
// are desired in a given context, use the algebraic sum
// type (list<T> | nothing) to indicate that the list may
// be empty.
// Note that the "next" member is either a reference to
// a list, or the special type "nothing".
// Note that we use generic syntax (angle brackets) and a
// dummy type placeholder "T" (which is arbitrarily chosen
// here) to denote that a list can hold elements of any
// type the programmer provides.
structure list<T> :
    T value,
    (list<T> ref | nothing) next

// Define a helper constructor for the list type
// This constructor creates a new list of one element
// given the value to place in the list. It is useful
// for constructing new lists without having to spell
// out the "nothing" in the "next" member.
list<T> : T value -> list<T> newlist = value, nothing [constructor]

// Prepend an element onto the beginning of a list
// This is a simple example of manipulating a data
// structure defined as above. Note again the use
// of generic syntax to indicate that the function
// can apply to any type.
prepend<T> : list<T> ref thelist, T value
    list<T> newlist = value, thelist
    thelist = newlist

// Append an element to the end of a list
append<T> : list<T> ref thelist, T value
    append_helper(thelist, thelist.next, value)

// Pattern matching base case: when appending to
// a list whose tail is nothing, replace the "next"
// reference with a new list containing the value
// to be appended to the original list.
append_helper<T> : list<T> ref thelist, nothing, T value
    thelist.next = list<T>(value)

// Pattern matching general case: when appending
// to a list which has a non-nothing tail, recurse
// until we reach the base case implemented above.
append_helper<T> : list<T> ref thelist, list<T> ref tail, T value
    append_helper(tail, tail.next, value)

// An example program using the list data structure
entrypoint :
    list<integer> numbers = 1
    append(numbers, 2)
    append(numbers, 3)


I'm also thinking of adding built-in tuple syntax and some other goodies. This sort of thing is starting to intrigue me, so I may wind up implementing it soon.

August 2015 »

23242526 27 2829