Solving the basic sum-of-a-sequence kata

19 views
Skip to first unread message

edA-qa mort-ora-y

unread,
Aug 18, 2017, 10:57:20 AM8/18/17
to Leaflang
This one wasn't so hard, but a necessary thing to cover. I had to add the `incl_range` iterator to the `std` library. We don't have the classic `for( begin, cond, iter )` statement. Mostly this is covered by such ranges instead, but we may consider adding it if needed.

Still not string formatting, that's a long way's off yet.

A defect remains here, in that `test_equal`, being a parametric, is instanced for each call made to it. There's no reuse of old instances it seems (judging from the output IR).


import std

defn sequence_sum = (start : integer, end : integer, step : integer ) -> ( total : integer ) {
	for v in std.incl_range( start, end, step ) {
		total = total + v
	}
}

defn test_equal = ( expect, value ) -> {
	expect == value else {
		std.print([ expect, " != ", value, "\n"])
	}
}

var main = -> {
	test_equal( 12, sequence_sum(2,6,2))
	test_equal( 15, sequence_sum(1,5,1))
	test_equal( 5, sequence_sum(1,5,3))
	test_equal( 45, sequence_sum(0,15,3))
	test_equal( 0, sequence_sum(16,15,3))
	test_equal( 26, sequence_sum(2,24,22))
}
Reply all
Reply to author
Forward
0 new messages