From 6567df27bb924d17a41b21ac012d88bfee4cbe34 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 18 Jun 2024 16:03:31 +0100 Subject: Added index function --- README.md | 1 + examples/sentences.fco | 6 +----- funco | 3 ++- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dd6fb76..68e4fbf 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ Other built-in functions are: * `drop(n list)`: return the list dropping the n first elements * `display(value)`: prints its arguments to the screen, for example `display("hello" "world")` * `..(i j)`: generate a list of integers from i to j +* `\[](l n)`: get item n in l (n is zero based) See the examples in `./examples`. diff --git a/examples/sentences.fco b/examples/sentences.fco index afb2d4f..6b6e448 100644 --- a/examples/sentences.fco +++ b/examples/sentences.fco @@ -3,11 +3,7 @@ def classify(xs) def sentence_type(s) def ends_with?(s c) - if =(tail(s) "") - =(s c) - else - @ends_with?(tail(s) c) - end + =([](s -(size(s) 1)) c) end if ends_with?(s "?") +(s " -> Q") diff --git a/funco b/funco index 86da6a6..808483a 100755 --- a/funco +++ b/funco @@ -427,7 +427,8 @@ def default_env(): "display": print, "..": lambda f, t: list(range(f, t + 1, 1) if f < t else range(f, t - 1, -1)) if ( isinstance(f, int) and isinstance(t, int) - ) else None + ) else None, + "[]": lambda i, n: i[n] } ) return env -- cgit v1.2.3