unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Python slices in Scheme
@ 2023-06-18 16:58 Damien Mattei
  2023-06-18 18:51 ` Dr. Arne Babenhauserheide
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Damien Mattei @ 2023-06-18 16:58 UTC (permalink / raw)
  To: guile-user

hello,

i'm porting the Python slicing (
https://docs.python.org/2/reference/expressions.html#slicings ) to Scheme
Guile and Racket.

examples in Scheme+ :

{#(1 2 3 4 5 6 7)[2 / 5]}
#(3 4 5)

i'm using / instead of : because : is already used by the SRFI 42 Eager
Comprehension

below are my testing examples:

;;; compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.6/usr/local/share/guile/site/3.0/Scheme+.scm.go

scheme@(guile-user)> {#(1 2 3 4 5 6 7 8)[/ / 3]}
$1 = #(1 4 7)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8)[/ / -2]}
$2 = #(8 6 4 2)
scheme@(guile-user)> {"elephant"[2 / 5]}
$3 = "eph"
scheme@(guile-user)> {"abcdefghijkl"[/ / -3]}
$4 = "lifc"
scheme@(guile-user)> {"123456789"[ /  / -1]}
$5 = "987654321"
scheme@(guile-user)> {"abcdefghijkl"[/ / 2]}
$6 = "acegik"
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[/ 7 / 2]}
$7 = #(1 3 5 7)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[/ 6 / -1]}
$8 = #(6 5 4 3 2 1)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[/ 6 / -2]}
$9 = #(6 4 2)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[/ -3 / -2]}
$10 = #(6 4 2)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[3 / / 2]}
$11 = #(4 6 8)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[3 / / -2]}
$12 = #(4 2)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[-3 / / 2]}
$13 = #(7 9)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[-3 / / -2]}
$14 = #(7 5 3 1)
scheme@(guile-user)> {"123456789"[/ -3 / -2]}
$15 = "642"
scheme@(guile-user)> {"abcdefghijklmno"[/ 7 / 2]}
$16 = "aceg"
scheme@(guile-user)> {"123456789"[/ -3 / -2]}
$17 = "642"
scheme@(guile-user)> {"abcdefghijklmno"[3 / / 2]}
$18 = "dfhjln"
scheme@(guile-user)> {"123456789"[3 /  / 2]}
$19 = "468"
scheme@(guile-user)> {"123456789"[3 /  / -2]}
$20 = "42"
scheme@(guile-user)> {"123456789"[-3 /  / -2]}
$21 = "7531"
scheme@(guile-user)> {"123456789"[-3 /  / 2]}
$22 = "79"
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[2 / 5 / 1]}
$23 = #(3 4 5)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[5 / 2 / -1]}
$24 = #(6 5 4)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[2 / 5 / -1]}
$25 = #()
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[-1 / 5 / -1]}
$26 = #(9 8 7)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[-0 / 5 / -1]}
$27 = #()
scheme@(guile-user)> {"0123456789"[5 / 2 / -1]}
$28 = "543"
scheme@(guile-user)> {"0123456789"[5 /  / -1]}
$29 = "543210"
scheme@(guile-user)> {"0123456789"[5 / 0 / -1]}
$30 = "54321"

the syntax is derived from Python syntax:

[1,2,3,4,5,6,7,8,9][-3::2]
[7, 9]
[1,2,3,4,5,6,7,8,9][-3::-2]
[7, 5, 3, 1]
[1,2,3,4,5,6,7,8,9][3::-2]
[4, 2]
"abcdefghijkl"[: : -3]
'lifc'
[1,2,3,4,5,6,7,8,9][:5:-1]
[9, 8, 7]
[1,2,3,4,5,6,7,8,9][:5:1]
[1, 2, 3, 4, 5]
[1,2,3,4,5,6,7,8,9][0:5:-1]
[]
[1,2,3,4,5,6,7,8,9][-0:5:-1]
[]
[1,2,3,4,5,6,7,8,9][-1:5:-1]
[9, 8, 7]
[1,2,3,4,5,6,7,8,9][:5:-1]
[9, 8, 7]
[1,2,3,4,5,6,7,8,9][10:5:-1]
[9, 8, 7]
[1,2,3,4,5,6,7,8,9][2:5:-1]
[]
[1,2,3,4,5,6,7,8,9][5:2:-1]
[6, 5, 4]
[0,1,2,3,4,5,6,7,8,9][5:2:-1]
[5, 4, 3]
[0,1,2,3,4,5,6,7,8,9][5::-1]
[5, 4, 3, 2, 1, 0]
[0,1,2,3,4,5,6,7,8,9][5:0:-1]
[5, 4, 3, 2, 1]

for now it works in simple expressions,soon it will be implemented for
expressions with LHS and RHS like {container1[2 / 5] <- container2[7 / 9]}
for this reason all will be in the next version of Scheme+
https://github.com/damien-mattei/Scheme-PLUS-for-Guile
with other features too.

Damien


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2023-06-24 20:24 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-18 16:58 Python slices in Scheme Damien Mattei
2023-06-18 18:51 ` Dr. Arne Babenhauserheide
2023-06-18 20:20   ` Damien Mattei
2023-06-18 21:44     ` Dr. Arne Babenhauserheide
2023-06-19  1:52 ` Jay Sulzberger
2023-06-19 16:36   ` lloda
2023-06-19 19:44     ` Zelphir Kaltstahl
2023-06-20  6:39     ` Damien Mattei
2023-06-23 13:26     ` Jay Sulzberger
2023-06-23 19:14     ` Damien Mattei
2023-06-20  6:41   ` Damien Mattei
2023-06-20 11:22     ` Robby Zambito
2023-06-21  7:45       ` Damien Mattei
2023-06-23 13:17 ` Jay Sulzberger
2023-06-24 13:08   ` Damien Mattei
2023-06-24 16:18     ` Dr. Arne Babenhauserheide
2023-06-24 20:24       ` Damien Mattei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).