For an assignment I need to have a program print the inputted number, and every number before it in sequence ex:
1
12
123
1234
12345
123456
I can get it to print a list of the number but for the love of god I cant get it to print the numbers before hand. I know theres a simple way to do it nesting a while loop in a for loop but I can't fucking figure it out.
>>8
Historical reasons. I thought C++ was always exempt from it, but I have a rule against reading other people's Sepples code.
Name:
Anonymous2010-04-17 18:27
>8
variable declarations in for statement are only valid in C99, not older C89/C90 or earlier nonstandard versions of C (and Visual C++ doesn't support C99 at, just something like C89/C90 and C++98/1x)
I'm too lazy to read every post, but >>1-13 YOU HELPED HIM!
I'm really getting tired of bitching about how much better /prog/ used to be, but really, every time I come on here these days there's nice little bundle of shitty threads to greet me. I can't help it; it's your problem, not mine.
>>13 variable declarations in for statement are only valid in C99
I'm surprised by that. Why bother with an initialization expression at all if you can't declare variables in it? That's its the idiomatic purpose. I wonder if there was a good reason not to support that.
Name:
Anonymous2010-04-17 19:26
>>16
in old C, variables could only be defined at the beginning of a block - this restriction was lifted in C99
>>13,17-19
I wrote the code and I didn't know that either. It's just a habit I picked up from a project where I kept sending code to be checked to a partner and he kept complaining that his compiler wouldn't accept the for loop declarations.
>>25
How is that different from a for loop, because it's not, besides DOTIMES is useful, and CL tries to include commonly used functions/macros in the language. While the core itself only has 25 special operators (some of which could be implemented through other special operators, but are kept as is for efficiency reasons), everything else can be built through functions and macros. There's no real reason to be minimalistic.
Here's how one could implement DOTIMES:
(defmacro dotimes ((var count-form &optional result-form) &body body)
(with-gensyms (count)
`(do ((,var 0 (1+ ,var))
(,count ,count-form))
((>= ,var ,count) ,result-form)
,@body)))
DO itself can be implemented as a macro, in form of LOOP (simple version), and LOOP itself can be implemented in form of TAGBODY and GO which are special operators. Common Lisp's goals are the same as most sane language's, that is, to help the programmer, and when they didn't think of something that you might want, give you enough tools so that you can easily build it yourself.
>>28
It just prints a newline. (princ #\newline) would do the same, or write-char, or something even more low-level if you wish...
The name itself is probably one of the worst function names ever, but it's just historical baggage. Most of the I just use FORMAT and ~% or ~&.
>>29
it's a function that only exists for the sake of toy programs. if you find yourself writing code to print a newline by itself very often, you're doing something very wrong.
;;; what one should write after reading a chapter of SICP (this is CL, but it's trivial to rewrite this in scheme, mostly by changing DEFUN/LABELS to DEFINE/LET and removing the lambda list stuff):
(defun print-pyramid-numbers (&optional (n 6))
(labels ((print-numbers (n &optional (i 1))
(cond
((= n i) (terpri))
(t
(princ i)
(print-numbers n (1+ i)))))
(rec-print (n &optional (i 1))
(when (>= n i)
(print-numbers i)
(rec-print n (1+ i)))))
(rec-print (1+ n))))
;;; What one would write after learning about LOOP:
(loop for i from 1 to 6 do
(loop for j from 1 to i do (princ j)
finally (terpri)))
Name:
Anonymous2010-04-18 4:33
void print_pyramid_numbers(int num) {
int i, j;
for(i = 1; i <= num; i++) {
for(j = 1; j <= i; j++) printf("%i", j);
printf("\n");
}
}
Name:
Anonymous2010-04-18 4:38
This thread has been closed and replaced with the following thread:
Subject:Data.List and List Name:
Email:
What's the difference between the two modules? The former seems to have few functions the latter doesn't.