Error Using Drakma for the Bing Search API Common Lisp - lisp

I am building a program that uses Bing's search API and common lisp with the Drakma library to display some results but for some reason have an error when sending a longer length query It doesn't display any of the results at all. It works for shorter length queries. I am using a temp account for this question. I have the following code.
(defun get-rid-of-spaces (var)
(cl-ppcre:regex-replace-all " " var "%20"))
(defun print-bing (search-term)
(format nil "https://api.datamarket.azure.com/Bing/Search/v1/Web?Query=%27~a%27&Options=%27DisableLocationDetection%27&$format=json&$top=1" (get-rid-of-spaces search-term)))
(defun drakma-bing (search-term)
(drakma:http-request (print-bing search-term)
:basic-authorization
'("bob.hammerston#mailinator.com" "L2gbaj+s1/KW/+ifAa9HrP0C1/kClpF4InH48Lw8UNc")))
(defun convert-to-string (response)
(map 'string #'code-char response))
And then I call this but it only works for short search terms and I can't figure out why. This doesn't work:
(convert-to-string (drakma-bing "what is the largest man in the world"))
But this does
(convert-to-string (drakma-bing "what is"))
Any idea why?
Thanks.
Edit:
I tried encoding the print-bing function by hand instead of using that function with a longer string and it still doesn't work so there must be an error with Drakma. I tried typing the domain into the web browser by hand and it works so that is why I think the error is with Drakma.

You need to use + instead of %20 for spaces.
(defun get-rid-of-spaces (var)
(cl-ppcre:regex-replace-all " " var "+"))

Related

Lisp complaining with invalid function

I write these code and compile withnewlisp. I wrote these code:
(defun getdone ()(format t "we have a IDE for cLisp"))
getdone()
and the error message
ERR: invalid function : (defun getdone () (format t "we have a IDE for cLisp"))
====================
I finally realized that it is syntax error because newlisp's grammar is different from clisp's grammar. Now my code is runing well:
(define (getdone) (format "we have a IDE for cLisp"))
(getdone)
I don't know what is the t in format t used for ?
The function call should be
(getdone)
not getdone().
If you're using newLISP, don't use
defun
use
define
to define all your functions.
if you're not using newLISP, your question's tags are wrong.

Emacs View Mail: trouble using vm-imap-account-alist

I have a little problem with ViewMail. In a separate file I'm defining variables with the account information of my email accounts, something like this:
(setq secret-mail-account-list-1 "imap-ssl:imap.aaa.com:993:*:login:xxx#aaa.com:password")
;;; other email accounts are defined as well
I do this to keep my password away from being recorded by Mercurial.
Then, I do write this into vm-imap-account-alist in another file:
(setq vm-imap-account-alist
'(
(secret-mail-account-list-1 "aaamail")
;;; other email accounts are here
)
)
But, after starting ViewMail with AltXvmEnter↵ I get the following error:
vm-imap-parse-spec-to-list: Wrong type argument: sequencep, secret-mail-account-list-1
How do I set the string to a variable so it is of the correct type for vm-imap-parse-spec-to-list?
I presume you got this from the EmacsWiki page. It looks like you want to pass the value of secret-mail-account-list-1 to vm-imap-account-alist rather than the symbol itself. To do so, you need to
backquote the alist and unquote the symbol with a comma to get its value:
(setq vm-imap-account-alist
`((,secret-mail-account-list-1 "aaamail") ; notice ` and ,
;;; other email accounts are here
))
At any rate, it helps to explain your error message: it looks like vm-imap-parse-spec-to-list expects a sequence (hence sequencep) such as a string which it converts to a list. Symbols are not sequences, but the content of yours is.

How can I make a Racket reader macro to transform any instance of one character into two other characters?

More specifically, I need any instance of | to be transformed into \|. I tried reading the documentation on reader macros and I am extremely lost. Can somebody explain this in understandable terms, or ideally show me how to write such a macro.
You're essentially destroying the usual meaning of | in the usual syntax, so you should really do that in some uniform way to minimize surprises. Your solution doesn't do that -- it treats | differently based on whether it's the first thing or not, and that's bad. For example, xxx|xxx reads it as a character in the whole symbol which means that you get a symbol with a name of "xxx|xxx", but with an input of |xxx you get two symbols, with names of "|" and "xxx" respectively. It's therefore better to just make your syntax treat | as other characters to get a more consistent and easier-to-explain behavior.
Another possible problem with your code is your use of #f as a readtable, which means that the reader is not going to compose with a reader that is potentially modified.
And finally, you should really pay attention to the extra arguments and return a syntax object when they're given. The reason for that is obvious, and my guess is that your reason for not doing so is that you're interested in just reading such code. But you should really have one eye looking at potential extensions, and in Racket, and obvious future for such code would be to make up a proper language instead of a read+eval hack. It's true that it might seem like an overkill, but in this case there is a simple solution of using the "same as this other character" shortcut to avoid writing lengthy code.
The following addresses all of these:
(define rt (let ([c (current-readtable)]) (make-readtable c #\| #\a c)))
Figured it out! Here is the code to adjust the (read) function to change all instances of | into \|:
(define rt2 (make-readtable #f #\| 'non-terminating-macro
(lambda (c in . _)
'\|)))
(parameterize ([current-readtable rt2]
[current-namespace ns])
(eval (read)))

Searching meaning of a word under cursor in Emacs using `dict` utility

Hi I am currently reading some novels in .txt format downloaded from Gutenberg.
Often I come across a word I am unfamiliar with, and I need to look it up on the web.
I recently stumbled upon the Unix dict command-line utility to search for meanings of words. It fetches
the meanings of words from multiples dictionaries and thesauruses on the web.
I would like to have this integrated with Emacs in the following way.
I would like to pass the word under the cursor to the dict and split the window into two buffers
: one buffer containing the current text-file and another-one the meaning of the words.
How would I write an Emacs-Lisp function for doing this?
NOTE: Using dict at the command-line is as simple as dict *word-being-searched*
and I am using Emacs-24 under Ubuntu 11.04
The word-at-point function does most of the work you need:
(require 'thingatpt)
(defun lookup-word-at-point ()
(interactive)
(shell-command (format "dict %s" (shell-quote-argument (word-at-point)))))
Like any other invocation of shell-command, this will show the output of dict in the minibuffer if it's short enough; otherwise, it will pop up a window named *Shell Command Output*.
ispell is your friend. ispell-word, bound by default to M-$ does exactly that.
Also see wordnet.el
EDIT: As Thomas pointed out, ispell is not appropriate. I am a big fan of wordnet that I mentioned earlier, you will need to download the program.
Another option is an interface to the dict command, e.g. enter link description here or one of the alternatives listed on that page.
There is enough stuff out there that you don't need to write something on your own.

Methods for solving common macro errors in Clojure

Basically I'm rather new to macros and I'm trying to work out how to write macros in Clojure. The problem is I keep getting exception errors and it's very difficult to figure out where to proceed. So really what I'm wondering is if I can get a list of either methods (or heuristics) for debugging Clojure macros.
Take the current problem I am working on, I have this model:
{:users [:name :email :registered-on]
:post [:title :author]}
and I want to convert it into the form:
(do (def new-form-users (cashew.core/new-form "/users/new" "Create a new Users"
["name" "email" "registered-on"] ("Name" "Email" "Registered-on")))
(def new-form-post (cashew.core/new-form "/post/new" "Create a new Post"
["title" "author"] ("Title" "Author"))))
for which I have written this macro:
(defmacro gen-create-forms [model]
`(do
~#(for [[entity-kw values] model]
(let [entity-sym (-> entity-kw name capitalize)
fields (vec (map name values))]
`(def ~(symbol (str "new-form-" (name entity-kw))) (new-form ~(str "/" (name entity-kw) "/new") ~(str "Create a new " entity-sym) ~fields ~(map capitalize fields)))))))
However when I run the macro I get:
java.lang.String cannot be cast to clojure.lang.IFn
[Thrown class java.lang.ClassCastException]
I've tried calling macroexpand-1, but I get the same error leaving me with little idea on how to solve it.
This tutorial provided by John Lawrence Aspden where he says "When the compiler sees a macro, which is just a function that returns some code, it runs the function, and substitutes the code that is returned into the program." prompted me to write the macro as a function which takes in the values I have and outputs the result that I want them to transform into.
Thus this works:
(defn gen-create-forms [model]
`(do
~#(for [[entity-kw values] model]
(let [entity-sym (-> entity-kw name capitalize)
fields (vec (map name values))]
`(def ~(symbol (str "new-form-" (name entity-kw))) (new-form ~(str "/" (name entity-kw) "/new") ~(str "Create a new " entity-sym) ~fields ~(map capitalize fields)))))))
(gen-create-forms {:users [:name :email :registered-on]
:post [:title :author]})
(do (def new-form-users (cashew.core/new-form "/users/new" "Create a new Users" ["name" "email" "registered-on"] ("Name" "Email" "Registered-on"))) (def new-form-post (cashew.core/new-form "/post/new" "Create a new Post" ["title" "author"] ("Title" "Author"))))
I'm not sure if I am using macros correctly here or if macros are the right strategy for solving this problem. However some ideas about what you do when faced with exceptions when writing a macro or good techniques for debugging them would be much appreciated.
EDIT: It has been brought to my attention by mikera that this is not a good example, however my question still stands. So to reiterate what techniques would you use when faced with an exception if coding a macro in clojure?
Personally I wouldn't use a macro for this - my proposed alternative would be:
Avoid trying to generate new symbol names in the namespace - this can get messy and complicated!
Instead create a single data structure called "new-forms" that contains all the forms in a hashmap. You could use either keywords or strings as keys, personally I'd use keywords like ":users" since you are already using that approach for your model data structure.
Generate "new-forms" with a function that takes the model as a parameter and calls (cashew.core/new-form ... ) for each form in the model as required
When you want to acess a specific form, you can then just do (new-forms :users) or similar to read the appropriate form out of the hashmap.