Author Topic: the duck is back..  (Read 3619 times)

0 Members and 1 Guest are viewing this topic.

Anonymous

  • Guest
the duck is back..
« on: May 03, 2004, 01:43:13 PM »
I having trouble trying to set up a list, input would help...
this is a crude beginning but I'm getting stumped with an important main part...that is, I want to make a calculation, store it to a variable, increment the variable, then store info to that one and so on...

Code: [Select]
(defun allnecks (/ A nxt_brch)
  ;; force an answer
  (initget 1)
  ;; get the size of the main duct, set to "main"
  (setq main (getreal "\nEnter duct size of main:")
;; set up loop
loop t
;; setup to start variable first is "branch A"
nxt_brch (chr 65)
)

  ;; start loop
  (while loop
    ;; get width of branch
    (setq wid (getreal "\nEnter target branch Width:")
 ;; get depth of branch
 dep (getreal "\nEnter target branch Depth:")
 )
    ;; mutliply width x depth and save to variable A and put into a list
    (setq branch (* wid dep)) ;<====not finished

    ;; if null response to width or depth, exit loop
    (if (null (or wid dep))
      (setq loop nil)
      )

    ;; increment variable and repeat
    (setq new_brch (1+ (ascii nxt_brch)))
    )
  ;; get sum of all variables in the list set to variable "total"

  ;; then divide each variable in the list by "total", and multiply by "main"

  ;; got to text screen and print out out all branches
  )

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
the duck is back..
« Reply #1 on: May 03, 2004, 02:04:20 PM »
maybe something along these lines
Code: [Select]

(defun allnecks   (/ A nxt_brch)
  ;; force an answer
  (initget 1)
  ;; get the size of the main duct, set to "main"
  (setq   main    (getreal "\nEnter duct size of main:")
   ;; set up loop
   loop    t
   ;; setup to start variable first is "branch A"
   nxt_brch (chr 65)
   cntr 1
   )

  ;; start loop
  (while loop
;; get width of branch
(if (setq wid (getreal "\nEnter target branch Width <enter to quit>:"))
 (if (setq dep (getreal "\nEnter target branch Depth:"))
(progn
 ;; mutliply width x depth and save to variable A and put into a list
 ;; make an assoc. list i.e.
 ;; branch = ((3 . 120.0) (2 . 294.0) (1 . 345.0))
 (setq branch (cons (cons cntr (* wid dep)) branch)
cntr   (1+ cntr)
)
 ;; increment variable and repeat
 (setq new_brch (1+ (ascii nxt_brch)))
 )
)
; else
 (setq loop nil)
 )
)
   
  ;; get sum of all variables in the list set to variable "total"

  ;; then divide each variable in the list by "total", and multiply by "main"

  ;; got to text screen and print out out all branches
  )
TheSwamp.org  (serving the CAD community since 2003)

Water Bear

  • Guest
the duck is back..
« Reply #2 on: May 03, 2004, 07:13:55 PM »
thanx Mark.. I'm just getting into mapcar and lambda..what have I done wrong here:
(setq total (mapcar '(lambda (x) (+ (cadr x) total)) branch))

I'm trying to extract the cadr of each dotted pair in "branch" and add them together

CarlB

  • Guest
the duck is back..
« Reply #3 on: May 03, 2004, 07:50:28 PM »
Try:
(apply '+ (mapcar 'cadr branch))

When 'mapcarring'? a single function you don't need the lambda bit.

Water Bear

  • Guest
the duck is back..
« Reply #4 on: May 03, 2004, 08:00:46 PM »
thanx CarlB but I don't believe that will work, because "branch" is a list of dotted pairs ... error: bad argument type: consp 100.0

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
the duck is back..
« Reply #5 on: May 03, 2004, 08:16:38 PM »
rather simplistic but gets the job done.  :D
Code: [Select]

(setq total 0)
(mapcar
  '(lambda (x)
     (setq one (cdr x))
     (setq total (+ one total))
     )
  branch
  )
TheSwamp.org  (serving the CAD community since 2003)

Water Bear

  • Guest
the duck is back..
« Reply #6 on: May 03, 2004, 09:43:11 PM »
thanx Mark..need a little push here, got this far but my final list isn't working.
Want it to read: branch #1 = x" neck
                        branch #2 = y" neck
Code: [Select]
(defun allnecks
       (/ main loop cntr wid dep branch one each neck main total)
  ;; force an answer
  (initget 1)
  ;; get the size of the main duct, set to "main"
  (setq main (getreal "\nEnter duct size of main:")
;; set up loop
loop t
;; setup to start variable
cntr 1
)

  ;; start loop
  (while loop
    (if (setq
 wid (getreal "\nEnter target branch Width <enter to quit>:")
 )
      (if (setq dep (getreal "\nEnter target branch Depth:"))
(progn
 ;; mutliply width x depth and save to variable A and put into a list
 ;; make an assoc. list i.e.
 ;; branch = ((3 . 120.0) (2 . 294.0) (1 . 345.0))
 (setq branch (cons (cons cntr (* wid dep)) branch)
cntr   (1+ cntr)
)
 )
)
      ;; else
      (setq loop nil)
      )
    )


  ;; get sum of all variables in the list set to variable "total"
  (setq total 0)
  (mapcar
    '(lambda (x)
       (setq one (cdr x))
       (setq total (+ one total))
       )
    branch
    )
  ;; got to text screen and print out out all branches

  (textscr)
  (setq cnt 1)
  (mapcar '(lambda (x)
    (prompt
      (strcat "\nBranch #"
      (rtos cnt 2 0)
      " = "
      (rtos (car x) 5 2)
      "\"neck"
      )
      )
    (setq cnt (1+ cnt));<== is this in wrong place?
    )
 (list (reverse (mapcar
  '(lambda (x)
     (setq each (cdr x))
     ;; then divide each variable in the list by "total",
                              ;; and multiply by "main"
     (setq neck (* main (/ each total)))
     )
  branch
  )
)
)
 )
  ;; end
  )

SMadsen

  • Guest
the duck is back..
« Reply #7 on: May 04, 2004, 07:27:58 AM »
Your problem is in the LIST function at the end.

Why not use the branch number that you already have as the CAR element in the dotted pairs?

Carl's suggestion for calculating total was OK ('cept for a minor typo) - it's implemented below along with outputting the branch number as it appears in the branch pairs. Added comments are tagged with "smd:"

Code: [Select]
(defun allnecks2 (/ branch cntr dep loop main total wid)
  ;; force an answer
  (initget 1)
  ;; get the size of the main duct, set to "main"
  (setq main (getreal "\nEnter duct size of main: ")
        ;; set up loop
        loop t
        ;; setup to start variable
        cntr 1
  )

  ;; start loop
  (while loop
    (if (setq wid (getreal "\nEnter target branch Width <enter to quit>: "))
      (if (setq dep (getreal "\nEnter target branch Depth: "))
        (progn
          ;; multiply width x depth and save to variable A and put into a list
          ;; make an assoc. list i.e.
          ;; branch = ((3 . 120.0) (2 . 294.0) (1 . 345.0))
          (setq branch (cons (cons cntr (* wid dep)) branch)
                cntr   (1+ cntr)
          )
        )
      )
      ;; else
      (setq loop nil)
    )
  )

  ;; get sum of all variables in the list set to variable "total"
  ;; smd: changed calculation of total to sum of all cdr's in branch
  (setq total (apply '+ (mapcar 'cdr branch)))

  ;; got to text screen and print out out all branches
  (textscr)
  (mapcar '(lambda (x)
             (prompt
               (strcat "\nBranch #"
                       ;; smd: output branch no. from list
                       (rtos (car x) 2 0)
                       " = "
                       ;; smd: output calculated neck from list
                       (rtos (cdr x) 5 2)
                       "\" neck"
               )
             )
           )
          ;; smd: list removed, code changed a bit and cleaned up
          (reverse
            (mapcar '(lambda (x)
                       ;; then divide each variable in the list by "total",
                       ;; and multiply by "main"
                       ;; smd: cons branch no. with calculation
                       (cons (car x) (* main (/ (cdr x) total)))
                     )
                    branch
            )
          )
  )
  ;; smd: silent exit added
  (princ)
  ;; end
)

Water Bear

  • Guest
the duck is back..
« Reply #8 on: May 04, 2004, 09:40:25 AM »
That cleared things up! thanx ..I guess that's what noObs do, add repetitive or unnecessary stuff