Skip to content

Instantly share code, notes, and snippets.

@y2q-actionman
Created September 14, 2018 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y2q-actionman/6ff62c755bd00ccf54fcae277925fa34 to your computer and use it in GitHub Desktop.
Save y2q-actionman/6ff62c755bd00ccf54fcae277925fa34 to your computer and use it in GitHub Desktop.
land of lisp には game-read という関数があるらしい
;;; http://programcat.hatenablog.com/entry/2018/09/13/213126
(in-package :cl-user)
;;; この定義すごい!、というかパッと読めない・・
(defun game-read ()
(let ((cmd (read-from-string
(concatenate 'string "(" (read-line) ")"))))
(flet ((quote-it (x)
(list 'quote x)))
(cons (car cmd) (mapcar #'quote-it (cdr cmd))))))
;;; let* で展開してみる
(defun my-game-read ()
(let* ((line (read-line))
(parened (concatenate 'string "(" line ")"))
(cmd (read-from-string parened)))
(flet ((quote-it (x)
(list 'quote x)))
(cons (car cmd) (mapcar #'quote-it (cdr cmd))))))
;;; loop 脳なので、すぐにこう書きたくなる。
(defun my-game-read-2 ()
(with-input-from-string (stream (read-line))
(loop for first? = t then nil
for i = (read stream nil :eof)
until (eq i :eof)
collect (if first?
i
(list 'quote i)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment