Login
7 branches 0 tags
Ben (X13/Arch) Added a [while] special form for efficient looping 3c9a220 4 years ago 84 Commits
nujel / stdlib / math.nuj
[def PI 3.14159]
[def π  3.14159]

[def ++ [λ [i]
	"Increment I by 1"
	[+  1 i]
]]

[def -- [λ [i]
	"Decrement I by 1"
	[+ -1 i]
]]

[def >> [λ [val amount]
	"Shifts VAL by AMOUNT bits to the right"
	[ash val [- amount]]
]]

[def min [let []
	[def iter [λ [a l]
		[cond [[nil? l] a]
		      [[< a [car l]] [iter a [cdr l]]]
		      [#t [iter [car l] [cdr l]]]
		]
	]]

	[λ [...l]
		"Returns the minimum value of its arguments"
		[cond [[nil? ...l] 0]
		      [[nil? [cdr ...l]] [car ...l]]
		      [#t [iter [car ...l] [cdr ...l]]]
		]
	]
]]

[def max [let []
	[def iter [λ [a l]
		[cond [[nil? l] a]
		      [[> a [car l]] [iter a [cdr l]]]
		      [#t [iter [car l] [cdr l]]]
		]
	]]

	[λ [...l]
		"Returns the maximum value of its arguments"
		[cond [[nil? ...l] 0]
		      [[nil? [cdr ...l]] [car ...l]]
		      [#t [iter [car ...l] [cdr ...l]]]
		]
	]
]]

[def fib [λ [i]
	   "Terribly inefficient, but useful for testing the GC"
	   [if [< i 2]
	       i
	       [+ [fib [- i 2]] [fib [- i 1]]]]
]]