我打算写一个简单的游戏来测试我对函数式编程的理解。执行主循环的功能方式是递归它,但是随着越来越多的堆栈帧的生成,这不会占用内存吗?
谢谢
没有可变状态的情况下如何做任何有用的事情?
// imperative version
pacman = new pacman(0, 0)
while true
if key = UP then pacman.y++
elif key = DOWN then pacman.y--
elif key = LEFT then pacman.x--
elif key = UP then pacman.x++
render(pacman)
// functional version
let rec loop pacman =
render(pacman)
let x, y = switch(key)
case LEFT: pacman.x - 1, pacman.y
case RIGHT: pacman.x + 1, pacman.y
case UP: pacman.x, pacman.y - 1
case DOWN: pacman.x, pacman.y + 1
loop(new pacman(x, y))