Home > Community > Blogs > Custom IC Design > skill for the skilled many ways to sum a list part 1

 Login with a Cadence account. Not a member yet? Create a permanent login account to make interactions with Cadence more conveniennt. Register | Membership benefits
 Get email delivery of the Custom IC Design blog (individual posts).

## Email

Recipients email * (separate multiple addresses with commas)

Message *

 Send yourself a copy

## Subscribe

Intro copy of the newsletter section here, some intro copy of the newsletter. Instruction of how to subscribe to this newsletter.

First Name *

Last Name *

Email *

Company / Institution *

 Send Yourself A Copy

# SKILL for the Skilled: Part 1, Many Ways to Sum a List

Comments(6)Filed under: Virtuoso, SKILL, SKILL++, software development, summing, sumlist, sum a list, Jim Newton, applyA while back I presented a one day SKILL++ seminar to a group of beginner and advanced SKILL programmers. One example I showed was Variations on how to sum a list of numbers. This is a good example because the problem itself is easy to understand, so the audience can concentrate on the solution techniques rather than on the problem itself.

I want to show a few of these examples in this blog post (and a few upcoming posts) in hopes you may find some of the techniques useful.

Summing Straightforward

One straightforward way to sum a given list of numbers is to use simple iteration and mimic a primitive microprocessor. That is, establish an accumulator initialized to zero, and continue to increment the accumulator by successive numbers from the list until the list is exhausted, and finally return the accumulated value.

```(defun sumlist_1a (numbers)
(let ((sum 0))
(foreach number numbers
sum = sum + number)
sum))
```

I imagine the code in this function would look very similar in many different languages such as C or Java -- of course with syntax variations.

Summing with apply

In SKILL++ (as well as in traditional SKILL) this is not a particularly efficient implementation because the SKILL virtual machine compiler, unlike a native compiler, is not able to make particularly good use of the processor registers. The way to force SKILL to use the processor registers is to organize your code, when possible, to call built-in compiled functions.

```(defun sumlist_1b (numbers)
(apply plus numbers))
```

Remember -- to force this function to be defined as SKILL++ rather than traditional SKILL, you'll either need to load it from a file with a .ils extension, or you'll need two wrap the definition in `(inScheme ...)` when you copy and paste it into the CIWindow.

A quick test with `measureTime` shows that `sumlist_1b` is roughly 10 times faster than `sumlist_1a` on a 10,000 element list of numbers.

How does it work?

The `plus` function returns the sum in its argument list. For example: `(plus 1 2 3 4 5)` evaluates to `15`.

The `apply` function, when called with two arguments, calls the function designated by its first argument. In particular `apply` calls that function argument list given as its second argument. For example:

`(apply plus '(1 2 3 4 5))`
is a call to `plus` with two arguments. The first argument `plus` is the function which `apply` should call. The second argument, `'(1 2 3 4 5)` is the list of arguments to call `plus` with. Thus this use of `apply` is equivalent to the following.
`(plus 1 2 3 4 5)`

Note that `plus` is used without a leading quote because in SKILL++ all global functions are available in a SKILL++ global variable of the same name. The variable `plus` evaluates to the plus function.

Since `sumlist_1b` is so much faster (execution-wise) and simpler (line-of-code-wise), it is very tempting to use, but be careful! It suffers from some limitations that do not exist with `sumlist_1a`. For example, `sumlist_1b` cannot sum the elements of the `nil` list. The `sumlist_1a`, however, claims the sum of the list `nil` is `0`, which indeed sounds like a reasonable answer. Furthermore, `sumlist_1a`, claims that the sum of a singleton list such as `(3.4)` is the first (and only) element of the list, which again seems like a reasonable result.

Apply in other languages

Actually the `apply` function is not an invention of the SKILL (or SKILL++) language. It is in fact central to programming languages derived from lambda calculus. A quick search on Wikipedia finds an article explaining its use in several languages.

More to come

In the next posting, I'll show some ways to get the speed and conciseness of `sumlist_1b` without the caveats.

http://en.wikipedia.org/wiki/Apply

Jim Newton

By Ram on September 6, 2012
Hi Jim,   Nice tutorials... I am looking for some extensive tutorials on mapcar, mapcan and other list traversing functions.      I am trying to add to list of points element vise.   eg.. '(1:2  3:4  5:6) + '(7:8 9:10 11:12) = '(8:10 12:14 16:18).  Is it possible to do this only with mapcar or maplist functions?  I understand that this can be done with foreach but its expensive is what I learnt from your tuts.

By Team SKILL on September 6, 2012
Hi Ram, thanks for the comment. I'll have to think about how to come up with a "extensive tutorial" on the mapping functions without being too esoteric.

In the mean time, here are a couple of functions you can try.  Remember to load this code from a file with a .ils extension.

(apply mapcar plus lists))

(add_lists '(1 2 3 4 5 6) '(2 3 4 5 6 7) '(0 1 2 3 4 5))

=> (3 6 9 12 15 18)

(add_points '((0 0) (1 2) (3 4) (5 6)) '((1 1) (2 2) (3 3) (4 4)))

=> ((1 1) (3 4) (6 7) (9 10))

Do you have a function in your virtuoso named: mapinto?  If so, you can implement add_points more efficiently with the caveat that it will destroy the given lists in generating the result.  If I'm not mistaken mapinto is available in IC615.

(apply mapinto (car lists) add_lists lists))

(apply mapinto (car lists) plus lists))

I'll let you ponder over the subtle implementations of add_points and add_lists above for a while and see if you can understand how they work.  Please play with them and experiment and let me know if you have more questions about them.

Kind Regards.

Jim

By Ram on September 6, 2012
Hi Jim,

Thanks for the prompt response. Your examples are working fine. Unfortunately my cadence version is 5.10...

I found that the two lists and their sublists have to be of same length  to work flawlessly. This is what I am trying to do.

assume a path with following points and selection values

aPath~>points = list(1:2 1:3 2:3)

geGetSelSetFigPoint(aPath) = (nil t nil)

and_lists(aPath~>points geGetSelSetFigPoint(aPath))

I'm expecting this result         list(nil 1:3 nil)

I hate to ask direct answer but these mapcar functions are bouncing.

Thanks a lot for accepting my request on tutorial topics. I'm looking forward.

-Ramakrishnan

By Ram on September 7, 2012
Hi Jim,        Thanks for the prompt reply. Your soultion helps.  Also thanks for accepting my request on the tutorial topics. Looking forward -Ramakrishnan

By Team SKILL on September 7, 2012
Hi Ram,  It seems like in your example, the fact that you have a list of points is not relevant.  You want a function which takes a list of objects (of any types), and a list of booleans, and returns a new list which does a logical AND of the respective elements.  Did I understand the question?

You can do this with (foreach mapcar ...) pretty easily.

`(defun mask_list (domain mask)  (foreach mapcar (item use?) domain mask    (and use? item)))`

Of course you can also write this with a direct call to mapcar rather than foreach.

The performance is identical, they differ only in surface syntax.

I personally find the (foreach ...) version a bit more readable.

`(defun mask_list (domain mask)  (mapcar (lambda (item use?) (and use? item))    domain mask))`

Also if you find the use of (and ...) difficult you can do the same thing with (when ..) which again should have the same performance characteristics.

`(defun mask_list (domain mask)  (foreach mapcar (item use?) domain mask    (when use? item)))`

With either version of the above function you should see that it works the same--selecting the items from the first list for which the corresponding item in the second list is non-nil.

`(mask_list '(A B C D E) '(nil t t nil t))`

==> (nil B C nil E)
`(mask_list '((0 0) (1 2) (3 4) (5 6) (7 8)) '(nil t t nil t))`

==> (nil (1 2) (3 4) nil (7 8))
Does this help?

By the way, sometimes you can get answers to questions like these by posting a message to comp.cad.cadence.

By Ram on September 8, 2012
Hi Jim,

This what I want. Thanks.