TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on November 24, 2004, 10:21:53 AM

Title: Question of the day #3
Post by: Mark on November 24, 2004, 10:21:53 AM
Re-write the following function using a different procedure but produce the same results.
Code: [Select]

(defun c:cyrds ( / cubic_yards)
  (if
    (setq cubic_yards
 (/
   (apply '*
  (list
    (getreal "\nEnter length: ")
    (getreal "\nEnter width: ")
    (getreal "\nEnter height: ")
    )
  )
   27.00
   )
 )
    (prompt
      (strcat
"\nCubic yards of concrete = "
(rtos cubic_yards 2 2)
)
      )
    )
  (princ)
  )
Title: Question of the day #3
Post by: MP on November 24, 2004, 10:34:22 AM
I'd prefer to leave the return value numerical so it could be used to feed another function or the command line. Dressing up the return value could be performed by a calling function.

Code: [Select]
(defun CubicYards ( )
    (apply '*
        (cons
            (/ 1 27.0)
            (mapcar
               '(lambda (what)
                    (initget 7)
                    (getdist (strcat "\nEnter " what))
                )    
               '("length: " "width: " "height: ")
            )
        )    
    )
)
Title: Question of the day #3
Post by: CAB on November 24, 2004, 10:47:00 AM
MP, very slick..
That one would be hard to beat.
Title: Question of the day #3
Post by: CAB on November 24, 2004, 11:11:45 AM
Here is my offering.
Rather straight forward.
Code: [Select]
(defun cyrds (bu / x y z)
  (initget 7)
  (setq x (getdist "\nEnter length: "))
  (initget 7)
  (setq y (getdist "\nEnter width: "))
  (initget 7)
  (setq z (getdist "\nEnter height: "))
  (/ (/ (* x y z) 27.0) (if (= bu "Feet") 1 1728))
)

(defun c:cyards(/ units)
  (initget "Feet Inches")
  (setq units (getkword "\nYou base unit is in [Feet or Inches] <Inches>."))
  (prompt (strcat  "\nCubic yards of concrete = " (rtos (cyrds units) 2 2)))
  (princ)
)
Title: Question of the day #3
Post by: Mark on November 24, 2004, 11:42:31 AM
While we're on the subject of nested functions..............
Code: [Select]

(defun c:cyrds ( / get-real cubic-yards val lst)

  (defun get-real (what)
    (getreal (strcat "\n"what": "))
    )

  (defun cubic-yards (lst)
    (/ (apply '* lst) 27.00)
    )

  (if
    (= (length
         (foreach item '("Length" "Width" "Height")
                  (if (setq val (get-real item))
                    (setq lst (cons val lst))
                    )
                  )
         )
       3
       )
    (princ
      (strcat "\nCubic yards = "(rtos (cubic-yards lst) 2 2))
      )
    ; else
    (princ
      (strcat
        "\nI need more than "
        (itoa (length lst))
        " numbers to complete my task"
        )
      )
    )
  (princ)
  )
Title: Question of the day #3
Post by: SMadsen on November 24, 2004, 01:21:13 PM
... and the recursive variant:
Code: [Select]
(defun cubicYards (lst)
  (cond ((= (length lst) 3)
         (mapcar 'princ (list (/ (apply '* lst) 27.0) " cubic yards")))
        ((cubicYards (cons (getdist (strcat "\nEnter "
                           (nth (length lst) '("length" "width" "height"))
                           ": ")) lst)))
  )
  (princ)
)
Title: Question of the day #3
Post by: hendie on November 24, 2004, 01:29:40 PM
Stig, what are you doing talking in old money ????
Title: Question of the day #3
Post by: SMadsen on November 24, 2004, 01:56:49 PM
Old money?

As for what I'm doing, I generally have no idea :D
Title: Question of the day #3
Post by: hendie on November 24, 2004, 01:59:46 PM
Quote from: SMadsen
Old money?

in the UK, when we refer to anything pre-decimal/pre-metric (i.e. feet and inches) it's referred to as "old money" like when we had £/s/d
Title: Question of the day #3
Post by: SMadsen on November 24, 2004, 03:12:18 PM
Ah ok. Thanks Hendie.

... they started it!