Code: Select all
> (empty? (array 2 '(1 2)))
ERR: list or string expected : (1 2)
Code: Select all
> (empty? (array 2 '(1 2)))
ERR: list or string expected : (1 2)
Yes, but we can't effectively create an empty array :TedWalther wrote:By default, we perceive an array as a special case of a list; so we expect it to act like a like in pretty much every way.
Code: Select all
newLISP v.10.7.1 32-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h
> (array 0)
ERR: wrong dimensions in function array : 0
>
+1 for pithy, direct answer. :)Lutz wrote:There are no empty arrays ;-)
Code: Select all
(define (is-blank-array x) (= x '()))
(define (is-list x) (list? x) (and (= (first x) "list") (list? (last x))))
(define (is-blank-hash x) (= x '(())))
(define (is-hash @x)
(and (list? @x) (for-all is-pair @x)))
(define (is-pair @x)
(and (list? @x) (= 2 (length @x)) (is-key (@x 0))))
(define (is-key @x) (or (string? @x) (number? @x)))
The name `is-blank-array` belies what the procedure is actually doing which is checking that its input is an empty list. In other words, it has nothing to do with arrays. Also, recall that Lutz said that there are no empty arrays in newLISP.ssqq wrote:I need make array, hash, list use newLISP. SO i do like follows:Code: Select all
(define (is-blank-array x) (= x '()))
The `(list? x)` expression is superfluous (i.e., does nothing). Moreover, this function doesn't do what its name seems to intend. Witness:ssqq wrote:Code: Select all
(define (is-list x) (list? x) (and (= (first x) "list") (list? (last x))))
Code: Select all
> (is-list (list 1 2 3))
nil
Code: Select all
> (is-list (list "list" 1 2 3))
nil
Code: Select all
> (last (list "list" 1 2 3))
3
Code: Select all
> (is-list (list "list" 1 2 (list 3)))
true
This doesn't "describe" a "hash" (i.e., hash table/map), but an association list. Lispers have called this "alist" for short. So, your function names might be better expressed as `empty-alist?` and `alist?`.ssqq wrote:Code: Select all
(define (is-blank-hash x) (= x '(()))) (define (is-hash @x) (and (list? @x) (for-all is-pair @x)))
In an alist, the keys can usually be more than just strings or numbers -- they can be whatever data in the language is comparable under certain equality operators; these operators will be the ones used in your alist lookup functions, by the way. In hash tables/maps, keys are anything that is "hashable" (i.e., any data for which the hash function can produced a hash).ssqq wrote:Code: Select all
(define (is-pair @x) (and (list? @x) (= 2 (length @x)) (is-key (@x 0)))) (define (is-key @x) (or (string? @x) (number? @x)))
I disagree. Why? Do you have a good use case?ssqq wrote:I think array should have empty array, Just like other language.
I think "Not empty array" is a design error.
The subject of the action is incorrect here IMO. It's not the `array-list` function that "sets up a correspondence"; it's your own mind that is doing this. My mind is not making the same correspondence. :DTedWalther wrote:The array-list function sets up a correspondance in the mind. This creates the expectation that arrays are just another type or format of list; so all the list functions should apply to it.
In my mind, arrays are *different* enough from lists ... :)TedWalther wrote:Contexts are different enough from lists that you learn to treat them as their own data type from the beginning. Arrays are similar enough to lists, that it is jarring when they don't act like lists.
Code: Select all
> (my @array [])
[]
> (@array << :a)
[:a]
> (my %hash {1 : 2, 3 : 4})
{1 ; 2, 3 : 4}
> (%hash 1)
2
> (my @lst :(1 2 3))
(1 2 3)