Skip to content

Instantly share code, notes, and snippets.

@nothingrealhappen
Created March 31, 2015 10:51
Show Gist options
  • Save nothingrealhappen/380e72a71f28e3ea9300 to your computer and use it in GitHub Desktop.
Save nothingrealhappen/380e72a71f28e3ea9300 to your computer and use it in GitHub Desktop.
20150331「每日一题」怕了么?
@nothingrealhappen
Copy link
Author

使用 lisp 或 js 写一个 stream

注: 永远返回一个 pair, (car (s)) 返回一个数, (cdr (s))则返回下一个 stream,
(car ((cdr (s))) 会返回第二个数, 可以无限的取下去.

写一个 stream 叫 funny-number-stream 
每次返回的是自然数(例 1, 2, 3, ...)
每到一个可以被5整除的数则变为负数(例: 1, 2, 3, 4, -5, 6, 7, 8, 9, -10, 11, ...)
注意: stream 是一个 thunk, 每次被 call 会产生一个 pair, car 是数字, cdr 是另外一个 stream

@flypiggy
Copy link

(= (car (funny-number-stream)) 1)
(= (car ((cdr (funny-number-stream)))) 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment