Author Topic: Question of the day #3  (Read 3493 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Question of the day #3
« 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)
  )
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Question of the day #3
« Reply #1 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: ")
            )
        )    
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Question of the day #3
« Reply #2 on: November 24, 2004, 10:47:00 AM »
MP, very slick..
That one would be hard to beat.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Question of the day #3
« Reply #3 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)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Question of the day #3
« Reply #4 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)
  )
TheSwamp.org  (serving the CAD community since 2003)

SMadsen

  • Guest
Question of the day #3
« Reply #5 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)
)

hendie

  • Guest
Question of the day #3
« Reply #6 on: November 24, 2004, 01:29:40 PM »
Stig, what are you doing talking in old money ????

SMadsen

  • Guest
Question of the day #3
« Reply #7 on: November 24, 2004, 01:56:49 PM »
Old money?

As for what I'm doing, I generally have no idea :D

hendie

  • Guest
Question of the day #3
« Reply #8 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

SMadsen

  • Guest
Question of the day #3
« Reply #9 on: November 24, 2004, 03:12:18 PM »
Ah ok. Thanks Hendie.

... they started it!