Jump to content

100 doors: Difference between revisions

1,209 bytes added ,  10 months ago
m
Line 7,607:
<syntaxhighlight lang="k">
(1+!9)^2
</syntaxhighlight>
 
=={{header|Koka}}==
Iterative version
 
<syntaxhighlight lang="koka">
type state
Open
Closed
 
fun toggle(self: state): state
match self
Open -> Closed
Closed -> Open
 
inline extern unsafe-assign : forall<a> ( v : vector<a>, i : ssize_t, x : a ) -> total ()
c "kk_vector_unsafe_assign"
 
fun main()
val doors = vector(100, Closed)
for(0,99) fn(pass)
var door := pass
while { door < 99 }
doors.unsafe-assign(door.ssize_t, doors[door].toggle)
door := door + (pass+1)
doors.foreach-indexed fn(idx, it)
match it
Open -> println("door " ++ (idx + 1).show ++ " is open")
Closed -> println("door " ++ (idx + 1).show ++ " is closed")
</syntaxhighlight>
 
Functional Version (Same definitions as above with different main)
 
<syntaxhighlight lang="koka">
fun main()
val doors = list(0,99,1,fn(i) Closed)
val transformed = list(1,99).foldl(doors) fn(drs, pass)
drs.map-indexed fn(i, door)
if ((i + 1) % pass) == 0 then door.toggle else door
transformed.foreach-indexed fn(idx, it)
match it
Open -> println("door " ++ (idx + 1).show ++ " is open")
Closed -> println("door " ++ (idx + 1).show ++ " is closed")
</syntaxhighlight>
 
24

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.