Example Pair copy 009

 

 

Description

This example shows how a Pair may be modified destructively or non-destructively using the the copy and setq function.

 

Details

The original Pair.

(define x '("The " "Rain " "in " "Spain") ) Returns: #<Pair 123456>

The copied Pair using the copy function.

(setq z (copy x)) Returns: #<Pair 234567>

The copied Pair using the setq function.

(setq twin x) Returns: #<Pair 345678>

After the first setq function, the values of Pair z are:

z[0] Returns: The
z[1] Returns: Rain
z[2] Returns: in
z[3] Returns: Spain

After the first setq function, the values of Pair x are:

z[0] Returns: The
x[1] Returns: Rain
x[2] Returns: in
x[3] Returns: Spain

The container named z is a copy of x (both are Pairs) and z has a separate memory space. However, the containers named x and twin point to the same memory spaces on the heap. Therefore

(setq twin[1] "Hail" ) Returns: #<Pair 123456>

The setq command results in the contents of both twin and x being both the same.

twin[1] Returns: Hail
x[1] Returns: Hail

Since z points to a different memory space, it still retained the original value which was copied from x before the values of x were modified.

z[1] Returns: Rain

Notes and Hints

Using the setq function results in objects having the same object id. But the object that is being assigned the copied object has a different object id than original object. In this example twin and x have the same object id while z has a different object id.

 

Related Examples

Here are examples of the copy function at work.

BitVector copy 011 Brick copy 012 CpxVector copy 013 Dictionary copy 002
Directory copy 003 FltVector copy 006 IntVector copy 007 Lambda copy 008
List copy 010 Matrix copy 006 Matrix copy 007 NumMatrix copy 011
NumVector copy 008 ObjVector copy 009 Pair copy 009 PcodeVector copy 010
ShortVector copy 008 Structure copy 001 Vector copy 004 Vector copy 005

 

Function Links

Here is the link to the current function used in this example.

copy

Here are a number of links to other related functions.

setq(#void)

 

Argument Types

Here are the links to the data types of the arguments used in this example.

Pair

Here are a number of links to examples having similar argument types.

BitVector isAtom 010 Dictionary isAtom 002 Directory isAtom 003 FltVector isAtom 005
IntVector isAtom 006 List associate 001 List c..r 001 List c..r 002
List car 001 List cdr 009 List compile 002 List count 007
List count 008 List eval 002 List eval 003 List inside 008
List isAtom 011 List isEqual 019 List isInside 010 List isMember 009
List isPair 002 List last 001 List length 015 List list 001
List makeQuotedList 001 List mapc 009 List member 009 List morph 001
List morph 002 List objectToList 001 List quote 001 List quote 002
List quote 003 List ref 028 List reverse 003 List setCar 002
List setCdr 011 List setLastCdr 011 List setq 025 List setq 026
Matrix isAtom 005 NumMatrix isAtom 012 NumVector isAtom 007 ObjVector isAtom 008
ObjectRepository isType 022 Pair append 011 Pair apply 009 Pair apply 010
Pair associate 001 Pair car 001 Pair compare 019 Pair compareEQ 019
Pair compareGE 019 Pair compareGT 019 Pair compareLE 019 Pair compareLT 019
Pair compareNE 019 Pair comparison 019 Pair cons 001 Pair copy 009
Pair count 006 Pair isAtom 010 Pair isEqual 018 Pair isIdentical 017
Pair isInside 009 Pair isMember 008 Pair isObject 014 Pair isPair 001
Pair isType 021 Pair length 014 Pair map 008 Pair mapc 008
Pair pair 001 Pair setCar 001 Pair sizeof 021 Pair type 023
PcodeVector isAtom 009 ShortVector isAtom 007 Structure isAtom 001 Vector isAtom 004

Analytic Information Server (AIS)

AIS Component Systems

  • Smartbase Engine
  • QT C++ Libraries
  • MySQL Relational Database
  • AIS Lisp Libraries
  • Rapid Analytic Demo IDE