Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - arunbear

#1
Anything else we might add? /
October 20, 2006, 02:35:13 PM
Hi Lutz, in Perl you can iterate through the (key, value) pairs of a dictionary without loading the whole keyset into memory



e.g.
my %dict = (name=> 'Aragorn' , alias=> 'Strider');
while(($key, $val) = each %dict)
{
    print "key = $key, value = $valn";
}


Python and Ruby have similar constructs.



How can this be done in newlisp? dotree would visit all symbols in the underlying context, even those that are not keys of the dictionary.
#2
Anything else we might add? /
October 16, 2006, 01:46:59 PM
Here it is as a module:



(context 'Dict)

(define (Dict:Dict key value)
   (if value
       (context (context) key value)
       (context (context) key)))

(define (get key) (Dict key))
(define (put key value) (Dict key value))

(define (keys)
    (map name (difference
                (symbols)
                (cons
                    (sym (string (context)) (context))
                    '(Dict get put keys key value values)))))

(define (values)
    (map get (keys)))

(context MAIN)

Usage:

> (new Dict 'd)
d
> (d "name" "fred")
"fred"
> (d "age" 10)
10
> (d:put "salary" 10000)
10000
> (d:get "salary")
10000
> (d:keys)
("age" "name" "salary")
> (d:values)
(10 "fred" 10000)
#3
Anything else we might add? /
October 08, 2006, 03:19:13 PM
The point is to localise x and y, not a and b.

e.g. suppose x and y were created with set rather than let:


(define (sum-sq a b)
    (set 'x (* a a))
    (set 'y (* b b))
    (+ x y))

(println (sum-sq 3 4))
(println x)
(println y)

In this case x and y continue to exist outside of the scope they were created in (whereas they would not in the let version).
#4
newLISP newS /
August 10, 2006, 02:46:30 PM
Quote from: "pjot"
Concerning Perl. Reading the PERL man-page (man perlsub):



"The Perl model for function call and return values is simple: all functions are passed as parameters one single flat list of scalars, and all functions likewise return to their caller one single flat list of scalars.  Any arrays or hashes in these call and return lists will collapse, losing their identities--but you may always use pass-by-reference instead to avoid this."





So by default, Perl passes by value.


Wrong. If you read further into persub, you will find this:
Quote
Assigning to a list of private variables to name your arguments:

sub maybeset {
    my($key, $value) = @_;
    $Foo{$key} = $value unless $Foo{$key};
}

Because the assignment copies the values, this also has the effect of turning call-by-reference into call-by-value. Otherwise a function is free to do in-place modifications of @_ and change its caller's values.

Try running Qrczak's snippet - code does not lie.