Skip to content

Instantly share code, notes, and snippets.

@rizo
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save rizo/331c5fe374ae1bbbabba to your computer and use it in GitHub Desktop.

Select an option

Save rizo/331c5fe374ae1bbbabba to your computer and use it in GitHub Desktop.
Playground with syntax alternatives for blocks in the Fold programming language.

Block definitions are used for compound expressions, in which the value of the expression is the value of the last expression in the block. Blocks can be used to structure local code inside a function or a module, perform effectful operations and make scoped definitions.

  • function
  • module
  • macro
  • type (?)
--
-- do-end notation
--

function assoc[T] v::PersistentVector[T] i::Int el
    boundscheck! v i
    if (i > |v| - |v.tail|):
        newtail = v.tail # (1..end)
        newtail # (mask i) := el
        PersistentVector v.trie newtail v.length
    else:
        newnode = v.trie[i][1:end]
        newnode # (mask i) := el
        PersistentVector (assoc v.trie i newnode) v.tail v.length
    end
end

function max list
    reduce (a b -> a > b ? a : b) list
end


type Tree[a] =
    | empty
    | node (a, Tree[a], Tree[a])
end

function map :: (a -> b) -> Tree[a] -> Tree[b]
function map f =
    | Empty => Empty
    | Node (x, l, r) => Node (f x, map f l, map f r)
end


--
-- { } notation
--

function assoc[T] v::PersistentVector[T] i::Int el = {
    boundscheck! v i
    if (i > |v| - |v.tail|) {
        newtail = v.tail # (1..end)
        newtail # (mask i) := el
        PersistentVector v.trie newtail v.length
    }
    else {
        newnode = v.trie[i][1:end]
        newnode # (mask i) := el
        PersistentVector (assoc v.trie i newnode) v.tail v.length
    }
}

function max list = {
    reduce (a b -> a > b ? a : b) list
}


type Tree[a] = {
    | empty
    | node (a, Tree[a], Tree[a])
}

function map :: (a -> b) -> Tree[a] -> Tree[b]
function map f = {
    | Empty => Empty
    | Node (x, l, r) => Node (f x, map f l, map f r)
}



--
-- indentation notation (= for block definitions)
-- (Maybe*)
--

function assoc[T] v::PersistentVector[T] i::Int el =
    boundscheck! v i
    if (i > |v| - |v.tail|):
        newtail = v.tail # (1..end)
        newtail # (mask i) := el
        PersistentVector v.trie newtail v.length
    else:
        newnode = v.trie[i][1:end]
        newnode # (mask i) := el
        PersistentVector (assoc v.trie i newnode) v.tail v.length

function max list =
    reduce (a b -> a > b ? a : b) list

type Tree[a] = empty
             | node (a, Tree[a], Tree[a])

function map :: (a -> b) -> Tree[a] -> Tree[b]
function map f =
    | Empty => Empty
    | Node (x, l, r) => Node (f x, map f l, map f r)


--
-- haskell-like notation
-- (Maybe)
--

assoc [T](v::PersistentVector[T] i::Int el) =
    boundscheck! v i
    if (i > |v| - |v.tail|):
        newtail = v.tail # (1..end)
        newtail # (mask i) := el
        PersistentVector v.trie newtail v.length
    else:
        newnode = v.trie[i][1:end]
        newnode # (mask i) := el
        PersistentVector (assoc v.trie i newnode) v.tail v.length

max list =
    reduce (a b -> a > b ? a : b) list

type Tree[a] = empty
             | node (a, Tree[a], Tree[a])

map :: (a -> b) -> Tree[a] -> Tree[b]
map f = | Empty => Empty
        | Node (x, l, r) => Node (f x, map f l, map f r)


--
-- indentation notation (expression definitions)
-- (No.)
--

assoc = [T](v::PersistentVector[T] i::Int el) ->
    boundscheck! v i
    if (i > |v| - |v.tail|):
        newtail = v.tail # (1..end)
        newtail # (mask i) := el
        PersistentVector v.trie newtail v.length
    else:
        newnode = v.trie[i][1:end]
        newnode # (mask i) := el
        PersistentVector (assoc v.trie i newnode) v.tail v.length

max = list ->
    reduce (a b -> a > b ? a : b) list

Tree = type[a]: empty
              | node (a, Tree[a], Tree[a])

map :: (a -> b) -> Tree[a] -> Tree[b]
map = f -> | Empty => Empty
           | Node (x, l, r) => Node (f x, map f l, map f r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment