Home > Community > Forums > Custom IC Design > Returning multiple variables in SKILL function

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 Returning multiple variables in SKILL function 

Last post Tue, Feb 26 2013 7:47 AM by theopaone. 9 replies.
Started by RFStuff 21 Feb 2013 09:49 AM. Topic has 9 replies and 2136 views
Page 1 of 1 (10 items)
Sort Posts:
  • Thu, Feb 21 2013 9:49 AM

    • RFStuff
    • Top 25 Contributor
    • Joined on Tue, Feb 5 2013
    • Posts 255
    • Points 4,510
    Returning multiple variables in SKILL function Reply

     Dear All,

    I want to a skill function that can return multiple values.

    Like: (a,b)=skillfunction(x,y,z....)

    Could anybody please tell how it can be done.

    Kind Regards,

     

             

     

    • Post Points: 20
  • Thu, Feb 21 2013 10:19 AM

    • skillUser
    • Top 10 Contributor
    • Joined on Fri, Sep 19 2008
    • Austin, TX
    • Posts 2,598
    • Points 16,075
    Re: Returning multiple variables in SKILL function Reply

    Hi,

    Looks like you might be used to Perl, but one way to do this in SKILL is to return a list of values, but you would have to assign them to variables outside of the function call in a more manual way.  Here's a brief outline:

    
      procedure(skillfunction(x y z)
        let( (ret1 ret2)
        ;; do some function on these parameters
        ret1 = x + 2*y
        ret2 = y - z
        ;; this next line is the return value of the function
        list(ret1 ret2)
        ); let
      ); procedure
    
      retlist = skillfunction(10 20 5)
      a = car(retlist)
      b = cadr(retlist)
    

    Something like that. Function arguments are separated by whitespace.  If you have more than a couple of variables then you might consider a more general approach, for example using a table structure for storage in named slots.

    Hopefully this is helpful to you.

    Regards,

    Lawrence.

    • Post Points: 20
  • Thu, Feb 21 2013 12:29 PM

    Re: Returning multiple variables in SKILL function Reply

    Returning multiple values as a list is a clean way to do this, but you can also use the destructuringBind function (new in IC615) to destructure the list contents and assign to variables (local to the scope of the destructuringBind function). This example uses the function that Lawrence wrote above to illustrate this:

    destructuringBind((a b) skillfunction(10 20 5)
      printf("a=%L b=%L\n" a b)
    )

    You can use this with other interesting list structures - for example, imagine that you have an instance id from the database - you can do:

    area=destructuringBind(((llx lly) (urx ury)) instId~>bBox
      ; compute the area of the bBox
      (urx-llx)*(ury-lly)
    )

    Cool, eh? (OK, so I'm easily impressed)

    Andrew.

    • Post Points: 35
  • Thu, Feb 21 2013 7:39 PM

    • kb how
    • Top 100 Contributor
    • Joined on Fri, May 6 2011
    • Melaka, Melaka
    • Posts 79
    • Points 1,075
    Re: Returning multiple variables in SKILL function Reply

    Interesting ...!

    Thanks for the sharing.

    -How-
    • Post Points: 5
  • Thu, Feb 21 2013 10:26 PM

    • RFStuff
    • Top 25 Contributor
    • Joined on Tue, Feb 5 2013
    • Posts 255
    • Points 4,510
    Re: Returning multiple variables in SKILL function Reply

     Dear  Lawrence & Andrew,

    Thanks a lot.

    Andrew: It is really nice. But is this feature available in IC5141.

    Kind Regards,

    • Post Points: 20
  • Thu, Feb 21 2013 10:40 PM

    Re: Returning multiple variables in SKILL function Reply

    In IC5141 you could use the following for the simple (non-nested list) case:

     (defmacro CCFsimpleMultiAssign (listOfVars listValue @rest body)
      `(apply (lambda ,listOfVars ,@body) ,listValue)
      )

    and then use CCFsimpleMultiAssign instead of destructuringBind in my first example. This won't work for the second bBox example - it would need a more complex macro (I threw the above together pretty quickly).

    Andrew.

    • Post Points: 20
  • Fri, Feb 22 2013 12:08 AM

    • RFStuff
    • Top 25 Contributor
    • Joined on Tue, Feb 5 2013
    • Posts 255
    • Points 4,510
    Re: Returning multiple variables in SKILL function Reply

    Dear Andrew,

    Thanks a lot.

    Kind Regards,

    • Post Points: 20
  • Sun, Feb 24 2013 5:34 PM

    • theopaone
    • Top 50 Contributor
    • Joined on Tue, Feb 12 2013
    • Cedar Park, TX
    • Posts 136
    • Points 2,050
    Re: Returning multiple variables in SKILL function Reply

     destructuringBind is wicked magic that should be studied in IC615. It is very useful, especially since you can return keyed values, optional values and @rest values.

    Before IC615, you could return the values as a disembodied property list (DPL). It is a list of key value pairs that begins with a nil:

    list( nil key1 "value1" key2 2.0 key3 list( "Value" "number" 3 ) )

    myDPL = callingMyFunction(....)

    myDPL->key1 => "value1"

    myDPL->key2 => 2.0

    myDPL->key3 => list("Value" "number" 3)

     Ted

     

    • Post Points: 20
  • Mon, Feb 25 2013 12:03 AM

    • RFStuff
    • Top 25 Contributor
    • Joined on Tue, Feb 5 2013
    • Posts 255
    • Points 4,510
    Re: Returning multiple variables in SKILL function Reply

    Dear Ted,

    Thanks a lot.

    Kind Regards,

    • Post Points: 20
  • Tue, Feb 26 2013 7:47 AM

    • theopaone
    • Top 50 Contributor
    • Joined on Tue, Feb 12 2013
    • Cedar Park, TX
    • Posts 136
    • Points 2,050
    Re: Returning multiple variables in SKILL function Reply

     You know you are in trouble if you dream in SKILL.

     In my last post, I made an error and realized it last night. The key values in a DPL must be symbols. In the example, they are variables and the code may fail but will definately not work the way it was written. The DPL should be written as:

     list( nil 'key1 "value1" 'key2 2.0 'key3 list( "Value" "number" 3 ) )

     The keys must be quoted to be symbols.

    Ted

     

    • Post Points: 5
Page 1 of 1 (10 items)
Sort Posts:
Started by RFStuff at 21 Feb 2013 09:49 AM. Topic has 9 replies.