faqts : Computers : Programming : Languages : Lisp

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 3 people (67%) answered Yes
Recently 2 of 3 people (67%) answered Yes

Entry

LISP: Allegro Franz Lisp: Microsoft: Windows: Simple: Function: How create function? [factorial]

Jan 25th, 2004 19:33
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 26 January 2004 - 00:08 am --------------------

LISP: Allegro Franz Lisp: Microsoft: Windows: Simple: Function: How 
create function? [factorial]

---

The more general form of a function is:

( defun <your function name>( your zero or more parameters separated 
by spaces )

 ; do something

)

---
---

e.g.


( defun mysimplefunction()
 ; do something
)

---
---

e.g. to create a factorial function, you could use:

( defun factorial( n )

  ( if ( <= n 1 )

      1

    ( * n ( factorial( 1- n ) ) ) )
)

---

Note it is necessary to write '1-'
(so not a space in between).

1+ and 1- are shorthand for adding and subtracting one from a number.

As stated, here in the factorial function you will have to use ( 1- n
), so no space between the '1' and the '-'.

---

This basically says:

integer function FNfactorialI( integer n ) {

  if n <= 1 {
   return( 1 )
  }
  else {
   return( n * FNfactorialI( n - 1 ) )
  }
 }

---
---

Steps: Overview:

 1. -Create a new application

     1. Run Allegro Lisp

 2. -Put a button on the form

     1. Click once on the leftmost button on the palette

     2. Click once on the form

     3. That will place a button on the form

 3. -Put some code in the event for this button

     1. Double click on the button

     2. -click button 'Events'
         in the 'Inspect' window

     3. -Double click on the 'On click' event line

     4. -That will add some default code

--- cut here ---------------------------------------------------------


;; Code for the dialog :form1

(in-package :common-graphics-user)


(defun form1-button4-on-click (dialog widget)
  (declare (ignore-if-unused dialog widget))

  t)

--- cut here ---------------------------------------------------------

 4. -To show the value of 3!, as title of the button, just add the
     following lines:

( defun factorial ( n )

  ( if ( <= n 1 )

      1

    ( * n ( factorial( 1- n ) ) ) )
)

 5. -So all together that gives the following code:

--- cut here ---------------------------------------------------------

;; Code for the dialog :form1

(in-package :common-graphics-user)

( defun factorial( n )

  ( if ( <= n 1 )

      1

    ( * n ( factorial( 1- n ) ) ) )
)

(defun form1-button4-on-click (dialog widget)

  (declare (ignore-if-unused dialog widget))

  ; assign to x the factorial: 'x = 3!'
  ( setq x ( factorial 3 ) )

  ; first search for the button4 in the existing controls,
  ; and assign this to a variable

    (setq mybutton (find-component :button4 (parent widget)))

 ; then set its property 'title'

    (setf (title mybutton) x)

  t)

--- cut here ---------------------------------------------------------

 6. -Run this code

     1. Click on the '>' in the menu

     2. Ignore the possible warning, by clicking the 'OK' button

     3. Then click on the button

        1. It will change its title to 3!, or thus 3 * 2 * 1, or thus
           '6'

            +----------------------+
            |                      |
            |           6          |
            |                      |
            +----------------------+

----------------------------------------------------------------------