How to use do in lisp? - function

What I'm trying to do is to write a function that which will restart a game of tic-tac-toe after it ends.
What is supposed to happen is that the player will be asked if they want to play another game. If they do, then they click yes and a function is called that will start the game over. If they choose no, then the program terminates.
Here is what i have so far...
(defun play-again ()
(do (y-or-n-p "Would you like to play again? ")
((play-one-game) nil)))
The play-one-game is the function that i want to run if the user clicks"yes" from the prompt. Otherwise the game terminates since no other function is called. By the way, just in case you need to know, i am using Allegro.

Assuming your y-or-n-p nil only when the user pressed n you could write your main loop as
(defun main ()
(loop :while (y-or-n-p "Would you like to play again? ")
:do (play-one-game)))
or at the end of play-one-game, which I would rename play, prompt the user and if he wants to play again just call play one more time.
(defun play-one-game ()
....
(when (y-or-n-p "Would you like to play again? ")
(play-one-game)))

If you don't understand a form in CL you should read in the CLHS (http://www.lispworks.com/documentation/lw60/CLHS/Body/m_do_do.htm)
Here a solution using do:
(defun play-again ()
(do ((again t (y-or-n-p "Would you like to play again? ")))
((not again) nil)
(play-one-game)))
(this function calls play-one-game the first time without a question)

Related

How to revert function definitions in Vim?

I don't know how to revert my function. Say I have this in .vimrc:
function! TempFunc()
" Temporary stuff
nnoremap v <c-v>
...
endfunction
silent! call TempFunc()
It'll call TempFunc(), right. I guess this function (and what inside it) will be deleted if I run command:
:delfunction! TempFunc
It deleting the TempFunc() but not including what its returned. So here v is still bound to <c-v>.
What I want is, if I delete TempFunc(), the code inside it will get deleted too and I lost my Temporary stuff. Any idea?
When you called TempFunc() it executed nnoremap v <c-v>. The mapping doesn't remember it came from the function so removing the function doesn't revert the mapping.
There is no single universal way to undo what a function have done — for every line in the function you must find its own way to undo.
In you case you need to unmap the mapping:
unmap v

Clojure: How to use Mapping with an Anonymous Function?

(defn recurse
[temp total] ;total is: (and true true(and false))
(map (fn [i]
(cond
(seq? i) (println "");If total is not a single parenthesis (single sequence), recur until it is
(= i 'and) (System/exit 0) ;I want this to be called only when the **second** "and" is called
:else (println "This should never print I think")
))
idealreturn)
)
I want (System/exit 0) to be called only when the second "and" is detected in total and not before. How would I go about doing this?
You are on the right track with mapping a function over the data to transform it. There are a couple of ways to get what you are looking for:
Don't use map, and use reduce instead. Reduce is for building up state over time. So you could reduce it into an expression, and each time you encounter an and, you look to see if there is already an and in the result you are building up, and if that and is already there, call the exit.
Have the function you are mapping over the input do only one thing, convert single items into more meaningful things. Then once it is done, pass that result to a second function that checks if it's time to exit.
Giving each thing one responsibility makes for code that's much easier to write, and composing them afterwords is efficient and easy. It's also much easier on you later when you come back to work on the code later.

Why is type not exiting correctly?

I am trying get Sikuli to open a chat window in a game(using t) and then close it without typing anything in (using enter).
But when I run the script Sikulu opens the menu and then types tt, then it closes the menu.
I put a delay of 1 second in between the type t and type enter in case it was running too fast but it did not help.
while True:
type("t")
sleep (1)
type(Key.ENTER)
I don't understand why are you running this in a loop. If you want to perform 2 actions:
Open chat (by hitting "t")
Close chat (by hitting "t")
Just do it outside the loop, simply like this:
type("t")
#some delay to ensure the menu has opened
type("t")
The proper way to do that though is to implicitly wait for the menu to appear. So if you can define a pattern that you can use as a reliable indicator, use it like this:
type("t")
wait(pattern, waitTime)
type("t")

In an Emacs Elisp function how to open a new buffer window, print a string to it, then close it with the keyboard press 'q'?

I am relatively new to elisp and I do not know how to phrase my question to find the answer on Google
I would like to define a function to:
open a new buffer
print a string into that buffer
close the buffer and window when the user focuses it and presses 'q'
What I have tried is
(defun test ()
(switch-to-buffer-other-window "*test*")
(erase-buffer)
(insert "hello from *test*")
(kill-this-buffer))
(test)
But that does not work the way I want it to.
For clarity, here is an image breakdown of what I would like the function to do:
Initial setup
The function test is executed
Focus remains on the initial buffer, then buffer named *test* is focused and q is pressed on the keyboard
The window configuration now does not have *test* in it and focus returns to the initial buffer
I plan on using this function to print my personal keybindings into the *test* buffer so I do not have to open my .emacs to see them
You might be looking for macro with-help-window:
(defun test ()
(interactive)
(with-help-window "*test*"
(princ "hello from test")))
If you want to be able to use insert instead of princ, then you can use this:
(defun test ()
(interactive)
(with-help-window "*test*"
(with-current-buffer "*test*"
(princ "hello from test"))))
(If you are using an older Emacs that does not have with-help-window then you can use with-output-to-temp-buffer instead.)
(The interactive is just so you can test it easily.)
You said:
I plan on using this function to print my personal keybindings
What about trying C-h b?
C-h b runs the command describe-bindings
as you can see if you type C-h k C-h b

Lwt and database access

I can't get my database access work with lwt. Should I include it in a thread? How? Or make a new thread which returns a 'a lwt value? If so, what to do with that value?
The same goes for Printf.eprintf, which also seems to be blocked by lwt. So I use Lwt_io instead. But why would lwt block regular io?
What I have is a simple db request like Db.update session. It is within an Lwt_main.run main function. All this is within a CGI script (should not matter, database access works fine until I start with the lwt commands).
I can give you more code if needed.
Regards
Olle
Edit
let main sock env =
(* code omitted *)
Gamesession.update_game_session env#db game_session_connected;
(* code omitted *)
Lwt_main.run (main sock_listen env)
Edit 2
This was the solution:
Lwt_preemptive.detach (fun () -> Db.call) ()
Printf.eprintf is not "blocked", it's just that the buffering parameters are changed and often messages do not display before the end of the program. You should try eprintf "something\n%!" (%! means "flush"), but yes it's better to use Lwt_io.
For the database, I don't know, you did not say which library you're using (at least the one called ocaml-mysql is not Lwt-friendly, so it may require using Lwt_preemptive).
Edit
Your:
Lwt_preemptive.detach (fun () -> Db.call) ()
This call creates a thread that, once executed, returns immediately the function Db.call. So, basically in that case Lwt_preemptive.detach does nothing :)
I don't know ocaml-mysql but if:
Db.call: connection_params -> connection_handle
you would have
let lwt_db_call connection_params =
Lwt_preemptive.detach Db.call connection_params