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.
or if you want it to look nice: Prelude Control.Monad> forM_[concatMap show[1..x]|x<-[1..7]]putStrLn
1
12
123
1234
12345
123456
1234567
Prelude Control.Monad>
Can someone please tell me why all these people are printing the numbers from 1 to 6 as if the OP didn't specify the number has to be inputted and it's not always 6?
Also what IS the difference between List and Data.List?
Name:
Anonymous2010-04-19 6:49
Also what IS the difference between List and Data.List? http://en.wikibooks.org/wiki/Haskell/Hierarchical_libraries When Haskell 98 was standardised modules were given a flat namespace. This has proved inadequate and a hierarchical namespace has been added by allowing dots in module names. For backward compatibility the standard libraries can still be accessed by their non-hierarchical names, so the modules List and Data.List both refer to the standard list library.
Name:
Anonymous2010-04-19 7:59
>>55 HAPPY NOW FAGGOT
ruby -e '(1..ARGV[0].to_i).each{|i|puts((1..i).to_a.join)}'
Name:
Anonymous2010-04-19 8:08
I love it how perl, considered here the most line noisy language among all popular provides the most concise and readable solution for the problem. Just have a look at that punctuation-fest ruby.
>>58
Readability depends very much on exposure to the language. When I saw the perl solution I was thinking "WTF is $_", although it was easy enough to figure out as we were given the solution. IMO, the most readable ones are in the C-style with explicit loops.
>>60
My point is that even knowing language, you still have to read the whole program to guess what it will do. The larger it is, the more you'll spend on processing it. say 1..$_ foreach 1..7 is the the easier to process for perl programmer than (1..ARGV[0].to_i).each{|i|puts((1..i).to_a.join)} is for ruby coder.
-accelerated c++
-the c++ programming language (the bible, form Stroustrup)
Name:
Anonymous2010-04-19 13:24
@69 Lol hy think you did my anus lol lets all do twitter style replies because it's so hipster and cool and written in ruby which is also hipster and cool
Arithmetic operations similar to those given below for the extended real numbers can also be defined, though there is no distinction in the signs (therefore one exception is that infinity cannot be added to itself). On the other hand, this kind of infinity enables division by zero, namely z/0 = \infty for any nonzero complex number z. In this context it is often useful to consider meromorphic functions as maps into the Riemann sphere taking the value of \infty at the poles. The domain of a complex-valued function may be extended to include the point at infinity as well. One important example of such functions is the group of Möbius transformations.