October 22nd, 2012, 02:04 PM
-
DrRacket procedure
I am working a bit with DrRacket and have a procedure I am having difficultly writing and executing. I want to take two ATOMS "X" and "N'. I want to add "X" to "X" for "N" number of times. i.e. for X is 2 and N is 5, I want 22222.
Thanks in advance for any help or suggestions you have
October 22nd, 2012, 06:25 PM
-
(BTW--you may attract more attention by saying "Scheme" instead of "DrRacket". Most of us recognize former, but need to look up the latter.)
It is helpful to see what progress you've made (and helps convince us you're not trying to get your homework done for you). Post code between [code]...[/code] tags to keep it legible. Also post any error messages you are getting (if any), what input you are supplying to the program, what you expect to happen and what happens instead. Additionally asking a specific question is more likely to get you help.
This page (So, You Need to Write a Program but Don't Know How to Start) has some helpful pointers on how to start on a program. In your case, you find it helpful to think about the following: Given "X", can I write a function that returns "XX"? Given a number N, can I write a function that runs N times?
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
October 24th, 2012, 07:32 AM
-
Schemer/DrRacket
Thanks OmegaZero. I did change the title in this reply as you suggested and below, to date, is my code.
<code>
(define duple
(lambda (n x)
(if (zero? n) '()
(cons (x)
(duple (- n 1) )))))
(duple 2 5)
</code>
October 29th, 2012, 08:00 PM
-
I know this is a few days old, but I figured I might still be able to help, here.
I see a few different problems with the code as written, mainly in that you seem to confuse the two arguments frequently. Perhaps some better names would help...
The biggest problem you'll run into is how you are calling (cons); right now, you are trying to pass it a x in a list (it should be n here, but that's besides the point), but the effect of it is that the interpreter tries to execute a function called 'x', and complains when it can't find one. The correct call form would be
Code:
(define duple
(lambda (original count)
(if (zero? count) '()
(cons original
(duple original (- count 1))))))
November 15th, 2012, 11:32 AM
-
P.S. As more background, I want to be able to do this from the command
line. So that's why I put the question that way. But also, I'm
interested in trying to use Racket from Emacs. At the moment the only
thing I really want now is "get the same thing as pressing F5 in
DrRacket". And (again maybe I'm missing something obvious, or botched
the config) neither Racket nor Geiser seem to provide just quite that.
So I was looking at binding F5 to mode-compile and setting the correct
Racket flags ... that I haven't figured out yet, because apparently
I'm a numskull.
Thank you.