mirror of
https://github.com/janet-lang/janet
synced 2025-07-01 17:43:04 +00:00
Updated Introduction (markdown)
parent
0a71e64668
commit
fa18463e10
@ -1,12 +1,12 @@
|
|||||||
# Hello, world!
|
# Hello, world!
|
||||||
|
|
||||||
Following tradition, a simple Dst program will simply print "Hello, world!".
|
Following tradition, a simple Dst program will print "Hello, world!".
|
||||||
|
|
||||||
```
|
```
|
||||||
(print "Hello, world!")
|
(print "Hello, world!")
|
||||||
```
|
```
|
||||||
|
|
||||||
Put the following code in a file call `hello.dst`, and run `./dst hello.dst`.
|
Put the following code in a file named `hello.dst`, and run `./dst hello.dst`.
|
||||||
The words "Hello, world!" should be printed to the console, and then the program
|
The words "Hello, world!" should be printed to the console, and then the program
|
||||||
should immediately exit. You now have a working dst program!
|
should immediately exit. You now have a working dst program!
|
||||||
|
|
||||||
@ -15,8 +15,8 @@ or read eval print loop. This is a mode where Dst functions like a calculator,
|
|||||||
reading some input from stdin, evaluating it, and printing out the result, all
|
reading some input from stdin, evaluating it, and printing out the result, all
|
||||||
in an inifinte loop. This is a useful mode for exploring or prototyping in Dst.
|
in an inifinte loop. This is a useful mode for exploring or prototyping in Dst.
|
||||||
|
|
||||||
This is about the simplest program one can write, and consists of precisely
|
This hello world program is about the simplest program one can write, and consists of only
|
||||||
three elements. This first element is the `print` symbol. This is a function
|
a few pieces of syntax. This first element is the `print` symbol. This is a function
|
||||||
that simply prints its arguments to standard out. The second argument is the
|
that simply prints its arguments to standard out. The second argument is the
|
||||||
string literal "Hello, world!", which is the one and only argument to the
|
string literal "Hello, world!", which is the one and only argument to the
|
||||||
print function. Lastly, the print symbol and the string literal are wrapped
|
print function. Lastly, the print symbol and the string literal are wrapped
|
||||||
@ -74,7 +74,7 @@ notation with a radix besides 10, use the `&` symbol to indicate the exponent ra
|
|||||||
|
|
||||||
Besides the 5 main arithmetic functions, dst also supports a number of math functions
|
Besides the 5 main arithmetic functions, dst also supports a number of math functions
|
||||||
taken from the C libary `<math.h>`, as well as bitwise operators that behave like they
|
taken from the C libary `<math.h>`, as well as bitwise operators that behave like they
|
||||||
do in C or Java.
|
do in C or Java. Functions like `sin`, `cos`, `log`, and `exp` will behave as expected to a C programmer.
|
||||||
|
|
||||||
# Strings, Keywords and Symbols
|
# Strings, Keywords and Symbols
|
||||||
|
|
||||||
@ -164,11 +164,8 @@ Functions can be defined with the `defn` macro, like so:
|
|||||||
(* base height 0.5))
|
(* base height 0.5))
|
||||||
```
|
```
|
||||||
|
|
||||||
A function defined with `defn` has a number of parts. First, it has it's name, triangle-area. This
|
A function defined with `defn` consists of a name, a number of optional flags for def, and
|
||||||
is just a symbol used to access the function later. Next is the list of parameters this function takes,
|
finally a function body. The example above is named triangle-area and takes two parameters named base and height. The body of the function will print a message and then evaluate to the area of the triangle.
|
||||||
in this case two parameters named base and height. Lastly, a function made with defn has
|
|
||||||
a number of body statements, which get executed each time the function is called. The last
|
|
||||||
form in the body is what the function evaluates to, or returns.
|
|
||||||
|
|
||||||
Once a function like the above one is defined, the programmer can use the `triangle-area`
|
Once a function like the above one is defined, the programmer can use the `triangle-area`
|
||||||
function just like any other, say `print` or `+`.
|
function just like any other, say `print` or `+`.
|
||||||
@ -183,15 +180,13 @@ nested inside a call to print), the inner function calls are evaluated first. Al
|
|||||||
a function call are evaluated in order, from first argument to last argument).
|
a function call are evaluated in order, from first argument to last argument).
|
||||||
|
|
||||||
Because functions are first-class values like numbers or strings, they can be passed
|
Because functions are first-class values like numbers or strings, they can be passed
|
||||||
as arguments to other functions as well
|
as arguments to other functions as well.
|
||||||
|
|
||||||
```
|
```
|
||||||
(print triangle-area)
|
(print triangle-area)
|
||||||
```
|
```
|
||||||
|
|
||||||
This prints the location in memory of the function triangle area. This idea can be used
|
This prints the location in memory of the function triangle area.
|
||||||
to build some powerful constructs purely out of functions, or closures as they are known
|
|
||||||
in many contexts.
|
|
||||||
|
|
||||||
Functions don't need to have names. The `fn` keyword can be used to introduce function
|
Functions don't need to have names. The `fn` keyword can be used to introduce function
|
||||||
literals without binding them to a symbol.
|
literals without binding them to a symbol.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user