Named parameters

Started by cormullion, March 01, 2008, 08:52:42 AM

Previous topic - Next topic

newBert

#15
Another solution with default arguments :


(define (func)
  (letn (lst (if (args) (first (args)))
          a (or (lookup 'a lst) 10)
          b (or (lookup 'b lst) 10)
          c (or (lookup 'c lst) 10))
  (println "a: " a )
  (println "b: " b)
  (println "c: " c)))

(func '((b 20) (c 40) (a 30)))
(func '((c 100)))
(func)

output:a: 30
b: 20
c: 40

a: 10
b: 10
c: 100

a: 10
b: 10
c: 10

:)
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

newBert

#16
And even better :


(define (func)
  (letn (lst (args)
          a  (or (lookup 'a lst) 10)
          b  (or (lookup 'b lst) 10)
          c  (or (lookup 'c lst) 10))
     (println "a: " a )
     (println "b: " b)
     (println "c: " c)))

(func '(b 20) '(c 40) '(a 30))
(func '(c 100))
(func)
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>