1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 01:57:09 +03:00

Added first, rest and nth

This commit is contained in:
Jos van Bakel 2017-05-12 22:15:14 +02:00
parent 86e32f4d5a
commit 19677091f1
2 changed files with 27 additions and 2 deletions

View File

@ -150,3 +150,28 @@ export ns = do
runtime-error "'concat' expected all parameters to be a list or vector"
{type: \list, value: params |> map (.value) |> concat}
'nth': fn (list, index) ->
check-param 'nth', 0, (list-or-vector list),
'list or vector', list.type
check-param 'nth', 1, index.type == \int,
'int', index.type
if index.value < 0 or index.value >= list.value.length
runtime-error 'list index out of bounds'
list.value[index.value]
'first': fn (list) ->
check-param 'first', 0, (list-or-vector list),
'list or vector', list.type
if list.value.length == 0
then const-nil!
else list.value[0]
'rest': fn (list) ->
check-param 'rest', 0, (list-or-vector list),
'list or vector', list.type
{type: \list, value: list.value.slice 1}

View File

@ -210,8 +210,8 @@ is-pair = (ast) -> ast.type in [\list \vector] and ast.value.length != 0
eval_quasiquote = (env, params) ->
# if params.length != 1
# runtime-error "quasiquote expected 1 parameter, got #{params.length}"
if params.length != 1
runtime-error "quasiquote expected 1 parameter, got #{params.length}"
ast = params[0]
new-ast = if not is-pair ast