ginger/README.md

117 lines
2.2 KiB
Markdown
Raw Normal View History

2014-04-16 16:39:24 +00:00
# Ginger
A lisp-like language built on the go programming language. The ideas are still a
work-in-progress, and this repo is where I'm jotting down my notes.
# Walkthrough
This is a number which evalutates to 5:
```
5
```
This is a string, as it contains no whitespace:
```
ImJustAString
```
This is also a string, it can contain anything:
```
"! I'm the king of the world !"
```
This is a list. It evaluates to a linked-list of four strings:
2014-04-16 16:39:24 +00:00
```
(a b c d)
```
This is a vector of those same elements. It's like a list, but has some slightly
different properties. We'll mostly be using lists:
2014-04-16 16:39:24 +00:00
```
[a b c d]
2014-04-16 16:39:24 +00:00
```
This is a string
2014-04-16 16:39:24 +00:00
```
+
2014-04-16 16:39:24 +00:00
```
`:` is the evaluator. This evaluates to a function which adds its arguments:
2014-04-16 16:39:24 +00:00
```
:+
2014-04-16 16:39:24 +00:00
```
This evaluates to list whose elements are a function and two numbers:
2014-04-16 16:39:24 +00:00
```
(:+ 1 2)
```
This evaluates to the number 5:
2014-04-16 16:39:24 +00:00
```
:(:+ 1 2)
```
The `fn` function can be used to define a new function. This evaluates to an
anonymous function which adds one to its argument and returns it:
2014-04-16 16:39:24 +00:00
```
:(:fn [x]
:(:+ :x 1))
2014-04-16 16:39:24 +00:00
```
The `def` function can be used to bind some value to a new variable:
2014-04-16 16:39:24 +00:00
```
:(:def foo bar)
# Now :foo will evaluate to the string bar
2014-04-16 16:39:24 +00:00
:(:def incr
(:fn [x]
:(:+ :x 1)))
# Now :incr will evaulate to a function which adds 1 to its argument
2014-04-16 16:39:24 +00:00
#defn is a shortcut for the above
:(:defn incr [x]
:(:+ :x 1))
2014-04-16 16:39:24 +00:00
```
There are also maps. A map's keys can be any value(?). A map's values can be any
value. This evaluates to a map with 2 key/val pairs:
2014-04-16 16:39:24 +00:00
```
{ foo :foo
bar (:incr 4) }
2014-04-16 16:39:24 +00:00
```
`.` is the half-evaluator. It only works on lists, and runs the function given
in the first argument with the unevaluated arguments (even if they have `:` in
front). You can generate new code to run on the fly (macros) using the normal
`fn`. This evaluates to a `let`-like function, except it forces you to use the
capitalized variable names in the body (utterly useless):
2014-04-16 16:39:24 +00:00
```
:(:defn caplet [mapping body...]
# elem-map maps over every element in a list, embedded or otherwise
::(:elem-map
:(:fn [x]
:(:if (mapping :(:slice :x 1))
(capitalize :x)
:x))
:body))
2014-04-16 16:39:24 +00:00
#Usage
.(:caplet [foo "this is foo"
dog "this is dog"]
:(:println :Foo)
:(:println :Dog))
2014-04-16 16:39:24 +00:00
```