starts-with question
Posted: Tue Oct 07, 2008 5:25 pm
starts-with now takes a list and expression as an argument. For example:
> (starts-with '((1 2) 3 4) '(1 2)) --> true
What is the logic for limiting this to just the first element? Doing so makes this little more than syntactic sugar for
> (= (first '((1 2) 3 4)) '(1 2)) --> true
Could this instead be a sublist match rather than membership so that the following would be true?
(starts-with '((1 2) 3 4) '((1 2) 3))
This one is more difficult to implement, but it can be done as follows:
> (set 'arg1 '((1 2) 3 4))
((1 2) 3 4)
> (set 'arg2 '((1 2) 3))
((1 2) 3)
> (= (0 (length arg2) arg1) arg2)
true
>
This could pose problems if implicit slicing out of bounds generates an error (it doesn't currently, but then neither did implicit indexing out of bounds not too long ago). Just imagine arg2 is longer than arg1. Also, things get more complicated with the corresponding ends-with version.
Broadly speaking, I am advocating support for sublist evaluations as well as membership/containment evaluations. This can get tricky when the potential sublist is situated below the first level of the list, but ultimately the judgment would be the same as for membership of an element.
Thanks,
John
> (starts-with '((1 2) 3 4) '(1 2)) --> true
What is the logic for limiting this to just the first element? Doing so makes this little more than syntactic sugar for
> (= (first '((1 2) 3 4)) '(1 2)) --> true
Could this instead be a sublist match rather than membership so that the following would be true?
(starts-with '((1 2) 3 4) '((1 2) 3))
This one is more difficult to implement, but it can be done as follows:
> (set 'arg1 '((1 2) 3 4))
((1 2) 3 4)
> (set 'arg2 '((1 2) 3))
((1 2) 3)
> (= (0 (length arg2) arg1) arg2)
true
>
This could pose problems if implicit slicing out of bounds generates an error (it doesn't currently, but then neither did implicit indexing out of bounds not too long ago). Just imagine arg2 is longer than arg1. Also, things get more complicated with the corresponding ends-with version.
Broadly speaking, I am advocating support for sublist evaluations as well as membership/containment evaluations. This can get tricky when the potential sublist is situated below the first level of the list, but ultimately the judgment would be the same as for membership of an element.
Thanks,
John