How to implement Python-style generator in Scheme (Racket or ChezScheme)?

前端 未结 4 1433
终归单人心
终归单人心 2021-01-03 07:46

today I solve the N-queen problem using Scheme but it is extremely slow compared to the same version of Python. when N = 8, Scheme takes 90+ seconds! I know one reason is th

4条回答
  •  情歌与酒
    2021-01-03 08:21

    I have found that do performs significantly more quickly than iterating over a list:

    (do ((i 0 (add1 i)))
      ((= i 100000) 'result)
       (some-function! i some-data))
    

    If you want to be more functional the Racket documentation suggests in-list for use with for and it's variants.

    (for/list ((i (in-list (range 0 100000))))
      (some-function i some-data))
    

提交回复
热议问题