RiSE4fun samples for KokaList of built-in samples for the Koka in RiSE4funen-USrise4fun &copy; 2017 Microsoft Corporationhttp://rise4fun.com//Images/Rise.gifRiSE4fun samples for Kokahttp://rise4fun.com/Koka/helloworldhelloworldfunction main() { println("hello effectful world of 効果! "); } http://rise4fun.com/Koka/fibfibfunction main() { println(fib(10)) } // Calculate the n'th fibonacci number function fib(n) { var x := 0 var y := 1 repeat(n) { y0 = y y := x+y x := y0 } return x } http://rise4fun.com/Koka/caesarcaesarfunction main() { println(caesar("koka is fun")) } // Caesar encode a string by shifting all lower-case letters 3 to the right. function caesar( s : string ) : string { s.encode( 3 ) } function encode( s : string, shift : int ) { fun encodeChar(c) { if (c < 'a' || c > 'z') return c base = (c - 'a').int() rot = (base + shift) % 26 return (rot.char() + 'a') } s.map(encodeChar) } http://rise4fun.com/Koka/nqueensnqueens// Calculate the solutions for the nqueens problem. module nqueens alias solution = list<int> alias solutions = list<list<int>> function safe( queen : int, diag : int, xs : solution ) : bool { match(xs) { Cons(q,qs) -> (queen != q && queen != (q+diag) && queen != (q - diag) && safe(queen,diag+1,qs)) _ -> True } } function appendSafe( queen : int, xs : solution, xss : solutions ) : div solutions { if (queen <= 0) xss else { if (safe(queen,1,xs)) then appendSafe( queen-1, xs, Cons(Cons(queen,xs),xss) ) else appendSafe( queen-1, xs, xss ) } } function extend(queen : int, acc : solutions, xss : solutions ) : div solutions { match(xss) { Cons(xs,rest) -> extend(queen, appendSafe(queen,xs,acc), rest) Nil -> acc } } function solutions( n : int, queen : int ) : div solutions { if (queen == 0) then [[]] else extend(n,[], solutions(n,queen-1)) } public function queens( n : int ) : div int { length(solutions(n,n)) } public function main() { println( "solutions: " + queens(8).show ) }