Skip to content

Commit

Permalink
feat: Add description for &rest
Browse files Browse the repository at this point in the history
  • Loading branch information
wwared committed Oct 1, 2024
1 parent 84ffa59 commit 94c13f1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/03-lisp.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ lurk-user> ((lambda (x y) (+ x y)) 3 5)
[7 iterations] => 8
```

Variadic functions are supported by adding `&rest <var>` at the end of the arguments list, which binds all a list of all remaining parameters to `<var>`.

```
lurk-user> ((lambda (&rest x) x))
[3 iterations] => nil
lurk-user> ((lambda (&rest x) x) 1 2 3)
[6 iterations] => (1 2 3)
lurk-user> ((lambda (x y &rest z) z) 1 2 3)
[6 iterations] => (3)
lurk-user> ((lambda (x y &rest z) z) 1 2)
[5 iterations] => nil
```

Lurk supports partial applications, so we can apply arguments one by one if we want.

```
Expand Down
21 changes: 21 additions & 0 deletions src/07-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ lurk-user> (atom '(1 2 3))
[2 iterations] => nil
```

### `apply`

`(apply f args)` will call `f` with the argument list `args` and return its result. Here `args` must be a list of the arguments.

```
lurk-user> (apply (lambda (x y z) (+ x (+ y z))) '(1 2 3))
[11 iterations] => 6
lurk-user> (apply (lambda (x y z) (+ x (+ y z))) (list 1 2 3))
[11 iterations] => 6
lurk-user> ((apply (lambda (x y z) (+ x (+ y z))) (list 1 2)) 3) ;; partial application also works
[12 iterations] => 6
```

### `begin`

`(begin e1 e2 ... en)` evaluates `e1`, `e2`, ..., `en` and returns the reduced version of `en`.
Expand Down Expand Up @@ -317,6 +330,8 @@ lurk-user> (if 0 "true") ;; note: `0` is *not* `nil`

`(lambda (args...) body)` creates a function that takes a list of arguments and returns the result of evaluating `body` by binding the variable bindings to the supplied arguments. A function is called when it is in the head of a list.

The list of arguments can optionally end with `&rest <var>`, which denotes that any remaining arguments, if present, are bound to `<var>` as a list.

```
lurk-user> (lambda () 1)
[1 iteration] => <Fun () 1>
Expand All @@ -330,6 +345,12 @@ lurk-user> ((lambda (x y z) (+ x (+ y z))) 1 2 3)
[10 iterations] => 6
lurk-user> (let ((f (lambda (x y z) (+ x (+ y z))))) (+ (f 1 2 3) (f 3 2 1))) ;; here `f` is the variable bound to the `lambda`
[19 iterations] => 12
lurk-user> ((lambda (x y &rest z) z) 1 2 3)
[6 iterations] => (3)
lurk-user> ((lambda (x y &rest z) z) 1 2)
[5 iterations] => nil
lurk-user> ((lambda (&rest x) x))
[3 iterations] => nil
```

### `let`
Expand Down

0 comments on commit 94c13f1

Please sign in to comment.