Ran this in SLIME and wondering why it waits on read input before outputting format.
(defun wage ()
(format t "~&Enter wage: ")
(let ((wage (read)))
(format t "~&Enter hours: ")
(let ((hours (read)))
(format t "~&Earned ~S dollars." (* wage hours)))))
* (wage)
2
Enter wage:
3
Enter hours:
Earned 6 dollars.
NIL
That happens because the standard output stream is buffered, which means that things that are printed to it do not actually write to the display straight away. You need to call (finish-output) before (read) in each instance to ensure that anything that's been buffered is written first.
Related
In this program I want to get the average of the entered numbers not only the sum but I can't get the average and the "Enter a number: " keeps repeating.
here's my code
(princ"Enter how many numbers to read: ")
(defparameter a(read))
(defun num ()
(loop repeat a
sum (progn
(format *query-io* "Enter a number: ")
(finish-output)
(parse-integer (read-line *query-io* )))))
(format t "Sum: ~d ~%" (num))
(format t "Average: ~d ~%" (/ (num) a)) ;; I can't get the output for the average and the "Enter a number: " keeps repeating.
Enter how many numbers to read: 5
Enter a number: 4
Enter a number: 3
Enter a number: 2
Enter a number: 1
Enter a number: 3
Sum: 13
Enter a number: <-------
The reason that your prompt is repeating is because your code is calling NUM twice. Each time NUM is called, it asks for more input. A basic fix is to wrap the part of your program that shows the results in a function, just calling NUM once and binding the results to a variable:
(princ "Enter how many numbers to read: ")
(defparameter a (read))
(defun num ()
(loop repeat a
sum (progn
(format *query-io* "Enter a number: ")
(finish-output)
(parse-integer (read-line *query-io*)))))
(defun stats ()
(let ((sum (num)))
(format t "Sum: ~d ~%" sum)
(format t "Average: ~d ~%" (/ sum a))))
This "works", but when the code is loaded the user is prompted for input to set the A parameter; then the user needs to call STATS to enter data and see the results. This is awkward to say the least. But there are lots of little problems with the program. Here are some suggestions:
Avoid using a global parameter at all
Call READ-LINE instead of READ to get the count of input elements
Use *QUERY-IO* consistently
Call FINISH-OUTPUT consistently
Use better variable names (A and NUM are not very descriptive here)
Use ~A instead of ~D
The average may not be an integer; it could be a fraction. When its argument is not an integer, FORMAT uses ~A in place of ~D anyway, but it is probably better to be explicit about this. I would just use ~A in both lines of output.
You can write one function to incorporate all of the above suggestions:
(defun stats ()
(format *query-io* "Enter how many numbers to read: ")
(finish-output *query-io*)
(let* ((count (parse-integer (read-line *query-io*)))
(sum
(loop repeat count
do (format *query-io* "Enter a number: ")
(finish-output *query-io*)
summing (parse-integer (read-line *query-io*)))))
(format *query-io* "Sum: ~A~%Average: ~A~%" sum (/ sum count))
(finish-output *query-io*)))
Here COUNT replaces the earlier A, and it is a local variable within the STATS function. LET* is used instead of LET so that SUM can make use of COUNT, but nested LET expressions could be used instead. Note that SUM is bound to the result of the loop, which is the result from the SUMMING keyword. *QUERY-IO* is used consistently throughout, and printed output is always followed by FINISH-OUTPUT when its sequencing is important.
There is a lot that could be done to further improve this code. There is no input validation in this code; that should be added. It might be good to break STATS into smaller functions that separate input, calculation, and output operations. It might be nice to be able to handle floats in input and output.
Sample interaction:
CL-USER> (stats)
Enter how many numbers to read: 3
Enter a number: 1
Enter a number: 2
Enter a number: 2
Sum: 5
Average: 5/3
OP has asked in a comment:
I tried to add (min count) (max count)) in your given code to get the
minimum and maximum value but the output is the sum. How can I get
minimum and maximum number?
This is really a new question, but it points to shortcomings in the design of the above function that were hinted at with "It might be good to break STATS into smaller functions...."
Firstly, note that (MIN COUNT) or (MAX COUNT) will not be helpful, since we want the minimum or maximum of the data entered; COUNT is just the number of values that the user wants to enter. The original code directly summed the values as they were input; a more flexible approach would be to collect the input in a list, and then to operate on that list to get the desired results. The MIN and MAX functions operate on a number of values, not on a list, so we will need to use APPLY to apply them to a list of results. We can also apply + to the list of input values to get the sum of the elements in the list:
(defun stats ()
(format *query-io* "Enter how many numbers to read: ")
(finish-output *query-io*)
(let* ((count (parse-integer (read-line *query-io*)))
(data
(loop repeat count
do (format *query-io* "Enter a number: ")
(finish-output *query-io*)
collecting (parse-integer (read-line *query-io*))))
(min (apply #'min data))
(max (apply #'max data))
(sum (apply #'+ data))
(avg (float (/ sum count))))
(format *query-io* "Minimum: ~A~%" min)
(format *query-io* "Maximum: ~A~%" max)
(format *query-io* "Sum: ~A~%" sum)
(format *query-io* "Average: ~A~%" avg)
(finish-output *query-io*)))
Here the loop collects input in a list by using the COLLECTING keyword, and the result is bound to DATA. The desired calculations are made, and the results are printed.
This works, but it is really begging for a better design; there are too many different things happening in one function. You could move the display code into another function, returning MIN, MAX, etc. in a list or as multiple values. But then it would be even more flexible if the calculations were made independent of the code that gathers input by allowing STATS to return a list of input. The calculation of AVG requires COUNT; the new code could return COUNT too, but it is not needed anyway, since we can call LENGTH on the input list to get a count. One wonders why we need the user to enter a number of elements at all. What if we took numbers from the user until a non-numeric value is entered?
This answer has already gotten quite long, but below is some code that breaks the STATS function into smaller parts, refining it in the process. By using smaller function definitions that are more focused on their tasks, the functions can be reused or combined with other functions to accomplish other goals more easily, and it will be easier to modify the definitions when changes are required. The final function, PROMPT-FOR-STATS-REPORT does essentially what the earlier STATS function did. It is now much easier to add new functionality by modifying PROMPT-FOR-STATS. New accessor functions can be added as needed when PROMPT-FOR-STATS is augmented, and PROMPT-FOR-STATS-REPORT can be modified to display results differently, or to access and display newly added functionality.
This is by no means the optimal solution to OP's problem, if there is an "optimal" solution. I encourage OP to try to find ways to improve upon the design of this code. There are some comments scattered throughout:
;;; Here is a function to prompt for an integer. Since we want to receive
;;; non-numeric input `:JUNK-ALLOWED` is set to `T`. When input that can not be
;;; parsed into an integer is provided, `PARSE-INTEGER` will now return `NIL`
;;; instead of signalling an error condition.
(defun prompt-for-int (msg)
(format *query-io* "~A" msg)
(finish-output *query-io*)
(parse-integer (read-line *query-io*) :junk-allowed t))
;;; Here is a function to prompt for input until some non-numeric value
;;; is given. The results are collected in a list and returned.
(defun prompt-for-ints (msg)
(format *query-io* "~A~%" msg)
(finish-output *query-io*)
(let (input-num)
(loop
do (setf input-num (prompt-for-int "Enter a number ENTER to quit: "))
while input-num
collecting input-num)))
;;; Some functions to calculate statistics:
(defun count-of-nums (xs)
(length xs))
(defun min-of-nums (xs)
(apply #'min xs))
(defun max-of-nums (xs)
(apply #'max xs))
(defun sum-of-nums (xs)
(apply #'+ xs))
(defun avg-of-nums (xs)
(float (/ (sum-of-nums xs)
(count-of-nums xs))))
;;; A function to prompt the user for input which returns a list of statistics.
(defun prompt-for-stats (msg)
(let ((data (prompt-for-ints msg)))
(list (count-of-nums data)
(min-of-nums data)
(max-of-nums data)
(sum-of-nums data)
(avg-of-nums data))))
;;; Accessor functions for a list of statistics:
(defun get-stats-count (stats)
(first stats))
(defun get-stats-min (stats)
(second stats))
(defun get-stats-max (stats)
(third stats))
(defun get-stats-sum (stats)
(fourth stats))
(defun get-stats-avg (stats)
(fifth stats))
;;; A function that prompts for input and displays results.
(defun prompt-for-stats-report ()
(let ((results (prompt-for-stats "Enter some integers to view statistics")))
(format *query-io* "Count: ~A~%" (get-stats-count results))
(format *query-io* "Minimum: ~A~%" (get-stats-min results))
(format *query-io* "Maximum: ~A~%" (get-stats-max results))
(format *query-io* "Sum: ~A~%" (get-stats-sum results))
(format *query-io* "Average: ~A~%" (get-stats-avg results))
(finish-output *query-io*)))
Sample interaction:
CL-USER> (prompt-for-stats-report)
Enter some integers to view statistics
Enter a number ENTER to quit: 1
Enter a number ENTER to quit: 2
Enter a number ENTER to quit: 1
Enter a number ENTER to quit:
Count: 3
Minimum: 1
Maximum: 2
Sum: 4
Average: 1.3333334
How do you convert a list into a string? I am trying to use parse-int to take a list of numbers and convert them to decimal, but i end up getting an error saying "The control-string must be a string, not (contents)".
I'm using format, but I'm not sure if I'm using it incorrectly.
Here's my code:
(princ "Enter a or list of hexadecimal numbers: ")
(setq numList (read-from-string (concatenate 'string "(" (read-line) ")")))
(defun hextodec(nums)
(setq numString (format "%s" nums))
(setq newNum (parse-integer numString :radix 16))
(write nums)
(princ " = ")
(write newNum)
) ;This will format the number that the user enters
(hextodec numList)
Since you're using read-from-string anyway, you can just tell Lisp's reader to read base 16 integers:
;; CLISP session
[1]> (let ((*read-base* 16)) (read-from-string "(9 A B C F 10)"))
(9 10 11 12 15 16) ;
14
read-from-string is a potential security hole if the contents of the string are untrusted input, because of the hash-dot evaluation notation.
When using the Lisp reader on untrusted data, be sure bind *read-eval* to nil.
[2]> (read-from-string "(#.(+ 2 2))")
(4) ;
11
Note how the #. notation causes the + function specified in the string data to be executed. That could be any function, such as something that whacks files in your filesystem, or sends sensitive data to a remote server.
Format's first argument is the stream to print to. You seem to intend to return the string instead of printing, so you should put nil there: (format nil "~s" nums)
The format control string "%s" does not contain any format directives. The entire format form does not make much sense here, as you seem to intend to loop over the given nums instead. You should employ some looping construct for that, e. g. loop, do, map, mapcar ….
I have the following function in my .emacs that notifies me after I've worked for a suitable amount of time.
Problem is, I'm unable to hardcode the values time and msg, so I have to reenter them each time.
(defun timed-notification(time msg)
(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
(run-at-time time
nil
(lambda (msg) (terminal-notifier-notify "Pomodoro" msg))
msg))
(setq column-number-mode t)
How do I set the time to always be "25 min" and the message to be "Take a break, time's up!"?
Here is my attempt:
(defun timed-notification()
;(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
(run-at-time 25
nil
(lambda ("Time's up")
(terminal-notifier-notify "Take a break, time's up!" msg))
msg))
(setq column-number-mode t)
Define your function like you did originally, then invoke it once with the parameters you want. The interactive form, like its name suggests, is only used when you actually invoke the function interactively. When you invoke it from code, you pass the parameters; so the interactive form is simply ignored.
(defun timed-notification (time msg)
(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
(run-at-time time nil (lambda (msg) (terminal-notifier-notify "Pomodoro" msg)) msg))
(setq column-number-mode t)
(timed-notification 25 "Take a break, time's up!") ;; New addition
You got rid of the msg parameter, but you were still trying to use it. Use let to bind a local variable to that value.
(defun timed-notification()
(interactive)
(let ((msg "Take a break, time's up!"))
(run-at-time 25 nil (lambda (mess) (terminal-notifier-notify "pomodoro" mess)) msg)))
(defun timed-notification (time msg)
(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
(run-at-time time nil (lambda (msg) (terminal-notifier-notify "Pomodoro" msg)) msg))
(setq column-number-mode t)
(defun tf()
(interactive)
(timed-notification "1 min" "Take a break, time's up!"))
Now tf can be called for a regular pomo, while the original function is still available for when I want an x-minute break.
I have this:
(defun promptread (prompt)
(format *query-io* "~10t~a:" prompt)
(force-output *query-io*)
(read-line *query-io*))
(defun prompt-cd ()
(make-cd
(promptread "Artist")
(promptread "Album")
(promptread "Rating")
(promptread "Like [y/n]")))
It works, however the format ~10t only affects the first call to promptread inside make-cd; the others are left-aligned without this padding.
Why would this be?
REPL:
CL-USER> (addcds)
Artist:Dixie
Album:Funny
Rating:22
The first promptread is indented because of the format with ~10t but not the others, which use the same exact format call.
The problem is that after force-output and readline, the cursor is not known to formatto be at position 0. Thus absolute tabulation will fail. If you start the format string with ~&, you will see this as an additional newline will be outputted anyway.
To solve the problem use the # modifier to get relative tabulation:
(format *query-io* "~10#t~a:" prompt)
I want to get output like 0 1, but the code below just print nil. I use type-of to test the (first hex), it is integer. %d should work, right? If I use message, it works in Emacs.
(defun draw-board (board)
(loop for x below 2
for hex = (aref board x)
do (format "%d " (first hex))))
(draw-board [(0 2) (1 2) (0 3) (0 2)])
1- emacs lisp format is not Common Lisp format. Notice the missing
argument!
(format "%d" 42) == (cl:format NIL "~D" 42)
2- therefore the only things your loop does are:
- to check that board is a vector with at least two slots. (aref
signals an error if the index is out of bound).
- to check that each of those two slots are lists (first signals an
error when passed a non list).
- to check that the first element of each each of those two slots
are numbers. (format signals an error when you pass a non number
for %d).
That's all.
You never said you wanted to print anything.
To print something, you must put it in a buffer, and use
ps-print-buffer:
(defun draw-board (board)
(with-temp-buffer
(loop for x below 2
for hex = (aref board x)
do (insert (format "%d " (first hex))))
(ps-print-buffer)))