Index: openacs-4/packages/acs-tcl/tcl/ad-functional-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/ad-functional-procs.tcl,v diff -u -r1.2 -r1.3 --- openacs-4/packages/acs-tcl/tcl/ad-functional-procs.tcl 15 Feb 2004 12:05:38 -0000 1.2 +++ openacs-4/packages/acs-tcl/tcl/ad-functional-procs.tcl 6 Jun 2004 08:59:29 -0000 1.3 @@ -46,7 +46,7 @@ # Lambda # -------------------------------------------------------------------------------- -ad_proc lambda {args body} { +ad_proc -public lambda {args body} { The lambda function - one of the foundations of functional programming - defines an anonymous proc and returns it. This is useful if you quickly need an auxiliary function for a small task. @@ -109,14 +109,14 @@ # as arguments to other functions. # -------------------------------------------------------------------------------- -proc + {a b} {expr $a + $b} -proc - {a b} {expr $a - $b} -proc * {a b} {expr $a * $b} -proc / {a b} {expr $a / $b} -proc && {a b} {expr $a && $b} -proc || {a b} {expr $a || $b} -proc > {a b} {expr $a > $b} -proc < {a b} {expr $a < $b} +proc + {a b} {expr {$a + $b}} +proc - {a b} {expr {$a - $b}} +proc * {a b} {expr {$a * $b}} +proc / {a b} {expr {$a / $b}} +proc && {a b} {expr {$a && $b}} +proc || {a b} {expr {$a || $b}} +proc > {a b} {expr {$a > $b}} +proc < {a b} {expr {$a < $b}} # Example: # + 5 6 = 11 @@ -125,7 +125,7 @@ # map # -------------------------------------------------------------------------------- -ad_proc map {f xs} { +ad_proc -public map {f xs} { Takes a function f and a list { x1 x2 x3 ...}, applies the function on each element of the list and returns the result, i.e. { f x1, f x2, f x3, ...}. @@ -235,7 +235,7 @@ # Standard combinators # -------------------------------------------------------------------------------- -ad_proc id {x} { +ad_proc -public id {x} { Identity function: just returns its argument.

I'm not kidding! An identity function can be useful sometimes, e.g. @@ -276,14 +276,14 @@ lambda {x} [list return $k] } -ad_proc curry {f args} { +ad_proc -public curry {f args} { Converts a function that takes one tuple as an argument into a function that takes a series of single arguments. } { uplevel [list $f $args] } -ad_proc uncurry {f tuple} { +ad_proc -public uncurry {f tuple} { Converts a function that takes a series of single arguments into a function that takes one tuple as an argument.

Example

@@ -355,7 +355,7 @@ # -------------------------------------------------------------------------------- ad_proc -public abs {x} "returns the absolute value of x" { - expr $x<0 ? -$x : $x + expr {$x<0 ? -$x : $x} } ad_proc -public gcd {x y} "returns the greatest common divisor of x and y" { @@ -364,29 +364,29 @@ proc gcd' {x y} { if { $y==0 } { return $x } - gcd' $y [expr $x%$y] + gcd' $y [expr {$x%$y}] } ad_proc -public lcm {x y} "returns the least common multiple of x and y" { if { $x==0} { return 0 } if { $y==0} { return 0 } - abs [expr $x/[gcd $x $y]*$y] + abs [expr {$x/[gcd $x $y]*$y}] } ad_proc -public odd_p {n} "returns 1 if n is odd and 0 otherwise" { - expr $n%2 + expr {$n%2} } ad_proc -public even_p {n} "returns 1 if n is even and 0 otherwise" { - expr 1-$n%2 + expr {1-$n%2} } ad_proc -public min {x y} "returns the minimum of x and y" { - expr $x<$y ? $x : $y + expr {$x<$y ? $x : $y} } ad_proc -public max {x y} "returns the maximum of x and y" { - expr $x>$y ? $x : $y + expr {$x>$y ? $x : $y} } # -------------------------------------------------------------------------------- @@ -480,14 +480,14 @@ } ad_proc -public take {n xs} "returns the first n elements of xs" { - lrange $xs 0 [expr $n-1] + lrange $xs 0 [expr {$n-1}] } ad_proc -public drop {n xs} "returns the remaining elements of xs (without the first n)" { lrange $xs $n [expr [llength $xs]-1] } -ad_proc filter {pred xs} { +ad_proc -public filter {pred xs} { Returns all elements of the list xs that fulfill the predicate pred.

Examples