Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
shrughes
Oct 11, 2008

(call/cc call/cc)

C-Euro posted:

How? My plan is

-Get date & time from computer, ideally hours:minutes:seconds:milliseconds or something
-Store each part of the time as a variable
-Set random number seeds to these values( iseed(1)=hour, iseed(2)=min, etc)

I know DATE_AND_TIME is something I need to use but I can't seem to find just how to use it- do I call it like a random number? Can I tell it to write somewhere?

Various pieces of documentation give examples for using it, see Google for details.

Adbot
ADBOT LOVES YOU

ufarn
May 30, 2009
I'm writing something in SML (functional programming), but I think it's a basic, general problem.

The problem is that my guards aren't working; they are parsed, but in the wrong way.

I am trying to make a check that ensures that my first argument consists of - only - eight digits.

code:
exception Under;
exception Over;

fun myfunc (0, n) = 0
  | myfunc (k, n) = if k > 99999999 then raise Over
                    else if k < 10000000 then raise Under
                    else myfunc(k div 10, n) * 10 + (k mod 10 + n) mod 10;

val proper = myfunc(12345678, 2);
val undertest = myfunc(2222, 4);
val overtest = myfunc(123456789, 2);
I am only supposed to raise one exception in case both criteria aren't met, but I just created Under and Over for debugging. The vals are test runs.

The under and over test work as they should - at least in terms of the exceptions they raise: respectively Under and Over. The seemingly correct parameter, however, raises the Under exceptions, which seems to be the general problem.

Ideally, I just want to write if 9999999 < k andalso k < 100000000 then raise Error else <script>, but for the time being, I'm trying to find out what the hell is wrong, and why I've wasted so many hours troubleshooting.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender
Your logic is confused. If you call myfunc with an eight-digit number as the first parameter, it will recursively call itself with that number divided by 10, which only has seven digits, and that recursive call will then raise the Under exception. What do you want to happen?

ufarn
May 30, 2009

ShoulderDaemon posted:

Your logic is confused. If you call myfunc with an eight-digit number as the first parameter, it will recursively call itself with that number divided by 10, which only has seven digits, and that recursive call will then raise the Under exception. What do you want to happen?
Thanks. I want to Caesar encrypt an 8-digit number with the cipher n (i.e. add n to all the numbers removing all carries in the process).

The guards that throw the exception Error are (1) n <= 0, (2) k not being eight digits.

EDIT: Think I've got it figured out now. Appreciate the help at a time where I'm completely wasted because I had to keep crunching, which resulted in stupid oversights like that. Will be back with more if anything else comes up. Still a little iffy about what
code:
fun myfunc (0, n) = 0
  | myfunc (k, n) = myfunc(k div 10, n) * 10 + (k mod 10 + n) mod 10
does step-by-step, although I thought/think I had it figured out before. Maybe I just need a good night's sleep.

ufarn fucked around with this message at 00:46 on Sep 16, 2010

soupbandit
Oct 24, 2006
Over the Horizon Radar
edit: wrong thread.

Gromit
Aug 15, 2000

I am an oppressed White Male, Asian women wont serve me! Save me Campbell Newman!!!!!!!
Here's a strange one:
I have a little custom hardware box that has a USB port to accept a keyboard. I travel with a laptop (amongst a bunch of other stuff) and was wondering if it were possible to have the laptop appear as a USB keyboard to this device. Maybe take over one of the USB ports to route key-presses through?
I don't use that box very often, and it's not worth adding a keyboard to my already-extensive kit just for that. Mind you, I've not priced those fold-up or rollable keyboards to see how cost-effective they are. But the less gear I need to take, the better, and I already have the laptop.

I presume this is possible, but I've really no idea. If such a thing does not already exist, it's possible you could sell it to the makers of the box in question, but I've no idea. They might not give a poo poo.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Gromit posted:

Here's a strange one:
I have a little custom hardware box that has a USB port to accept a keyboard. I travel with a laptop (amongst a bunch of other stuff) and was wondering if it were possible to have the laptop appear as a USB keyboard to this device.

Almost certainly not. The hardware size of USB is different for master and slave devices, and your laptop probably does not have hardware capable of handling the slave role. Some netbooks might.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
IIRC there was at least one emacs person lingering around here that was using CEDET to help them develop. I installed it and set it up and haven't done a single thing with it. I was hoping to at the least use it to try to help my autocomplete stuff for some GUI work I'm doing with FLTK. If I was given a list of methods for a particular widget, I could probably figure out what I need to call. But as it stands, I have to look up the documentation for the widget I'm using and get the exact name and prototype.

I assume there's a command I can use to have it pitch stuff to me. As it stands, all this stuff seems to be loaded w/o doing anything.

Gromit
Aug 15, 2000

I am an oppressed White Male, Asian women wont serve me! Save me Campbell Newman!!!!!!!

ShoulderDaemon posted:

Almost certainly not. The hardware size of USB is different for master and slave devices, and your laptop probably does not have hardware capable of handling the slave role.

I presume you mean "side" there, right?
I was hoping that "universal serial bus" would be very universal, and be fully bi-directional and just down to what data is sent. Guess I'll look into little fold-up keyboards or something.

pseudorandom name
May 6, 2007

Even if he did mean "side" instead of "size", master and slave USB connectors are differently shaped specifically to prevent you from connecting host-to-host or device-to-device..

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender
i r gud at typin

Yeah, I meant "side". USB was designed to be as cheap as possible, and as a result the processing requirements are very different on the two sides, and most controllers can't handle both.

Firewire is symmetrical, for what that's worth.

Gromit
Aug 15, 2000

I am an oppressed White Male, Asian women wont serve me! Save me Campbell Newman!!!!!!!

pseudorandom name posted:

...master and slave USB connectors are differently shaped specifically to prevent you from connecting host-to-host or device-to-device..

Well yeah, but that's only a cable issue, and it's easy enough to get something with the right plugs on it.

ante
Apr 9, 2005

SUNSHINE AND RAINBOWS
I've got a bit of a complicated situation.

I have a closed source application(HAPPY.exe, say) that calls a DLL I've written once every second. I'd like to have another application(SAD.exe :() send messages/commands to the DLL.

The crappy way that I've got it working right now is communicating through textfiles in the same folder. Yeah, hackish and bad. Is there some way that I can send information to a stack/queue or something in the dll?

So I have the source for the DLL and SAD.exe, but not HAPPY.exe.
How I've been considering doing it is using a code cave in the DLL or HAPPY.exe, which I can totally pull off, but I'd rather an easier and more acceptable method.

litghost
May 26, 2004
Builder

ante posted:

I've got a bit of a complicated situation.

I have a closed source application(HAPPY.exe, say) that calls a DLL I've written once every second. I'd like to have another application(SAD.exe :() send messages/commands to the DLL.

The crappy way that I've got it working right now is communicating through textfiles in the same folder. Yeah, hackish and bad. Is there some way that I can send information to a stack/queue or something in the dll?

So I have the source for the DLL and SAD.exe, but not HAPPY.exe.
How I've been considering doing it is using a code cave in the DLL or HAPPY.exe, which I can totally pull off, but I'd rather an easier and more acceptable method.

Err, you have some misconceptions about what a DLL is. DLL's and EXE's are just blobs of executable code (you functions, main, etc) and data (e.g. constants). What you are talking is about inter-process communication or IPC. In particular it sounds like you want to facilitate IPC through a DLL. To this all you need to do is put the state data for the DLL in some form of fixed shared location. A file is sort of what you want, but I bet what you are really looking for is a memory mapped file or shared memory (or a message queue depending). As long as you DLL code loads the same memory mapped file or shared memory, every call to DLL will see that state. Just make sure you properly serialize (named mutexs or semaphores) access to the data, otherwise your app will run into race condition and other weird and untraceable behavior.

Also worth noting the "lifetime" of the data is important. If you need to have data persistent when the DLL in not in memory (and it unloads its shared memory handles), you may need to force a persistent store against the hard-drive. However if there is at least one process attached to the shared memory, it is less of an issue.

Links:
For Windows
For Linux
A cross platform solution

litghost fucked around with this message at 00:04 on Sep 19, 2010

ante
Apr 9, 2005

SUNSHINE AND RAINBOWS

litghost posted:

Err, you have some misconceptions about what a DLL is.

I just didn't really know how to phrase exactly what I wanted, but yeah. This looks perfect, thanks.

loinburger
Jul 10, 2004
Sweet Sauce Jones
I've written a windows batch:

set environment_variable=whatever
[program].exe /options

If I call it from the command line then the environment variable is set and the program executes. If instead I double-click the .bat file from the Windows (XP or 7) gui then the environment variable isn't set though the program still executes. I'm making this .bat file for people who aren't comfortable with the command line, and so I'd like them to be able to double-click the .bat file from the gui. What gives?

RobotEmpire
Dec 8, 2007
I hope this is a good place for this question.

I just started working through Structure and Interpretation of Computer Programs, and am on problem 1.11, which reads as the following:

quote:

Exercise 1.11. A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.

This is all to be done in Scheme of course.

So after awhile I was beating my head against a wall and went ahead and cheated by looking up the answer:

code:
(define (foo n) 
   (define (foo-iter a b c n) 
     ;; a = f(n - 1), b = f(n - 2), c = f(n - 3). 
     ;; return a + 2b + 3c 
     (if (< n 3) 
         a 
         (foo-iter (+ a (* 2 b) (* 3 c)) a b (- n 1)))) 
   (if (< n 3) 
       n 
       (foo-iter 2 1 0 n)))


I still didn't get it, so I decided to go ahead and write out the function long-hand using f(5) as an example:

code:
f(5) = f(5-1) + 2f(5-2) + 3f(5-3)
	= f(4) + 2f(3) + 3f(2)
	= [f(3) + 2f(2) + 3f(1)]+2[f(2)+2f(1)+3f(0)]+6
	= [f(3) + 2(2) + 3(1)] + 2[2+2(1) + 3(0)]+6
	= [f(3) + 4 + 3)] + 2(4) + 6
	= (f(3) + 7) + 8 + 6
	= (f(3) + 7) + 14
	= f(3) + 21
	= (f(2) + 2f(1) + 3f(0)+ 21
	= (2+2+0) + 21
	= 4+21
	= 25
This work checks positively against using the function above but there may still be errors I reckon.

The problem is -- I *still* don't understand what the iterative example is doing. The recursive function is obviously very easy to write, it's just a straight English -> Scheme translation. But the iterative example solution has me boggled. I do not understand what's going on. I've spent almost every available free moment to think on it over the past few days and I still don't get it. Can someone take a shot at explaining?

edit: Whoops, I tried to spoiler the solution but I guess spoiler and code tags don't play nice.

RobotEmpire fucked around with this message at 01:43 on Sep 21, 2010

shrughes
Oct 11, 2008

(call/cc call/cc)

RobotEmpire posted:

The problem is -- I *still* don't understand what the iterative example is doing. The recursive function is obviously very easy to write, it's just a straight English -> Scheme translation. But the iterative example solution has me boggled. I do not understand what's going on. I've spent almost every available free moment to think on it over the past few days and I still don't get it. Can someone take a shot at explaining?

Evaluate the iterative solution by hand and see what it does?

tef
May 30, 2004

-> some l-system crap ->
It uses an accumulator in the arguments to carry values into recursive statements:

roughly, it translates
f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n>= 3.
into
code:

f(n) = n if n < 3 and f(2,1,0,n)

f(a,b,c,m) = a if m < 3 
         and f(a+2*b+3*c,m-1) if m >= 3 
the arguments a,b,c represent the values of f(2), f(1), f(0) initially.
each time we recurse we replace the values with the next f(...) in sequence

so, after one loop a,b,c become f(3), f(2), f(1), and after two a,b,c become f(4), f(3), f(2)

we want to recurse until a=f(n).

we do this by using m to represent how many steps to take: for n=2 we can return a, for n=3 we must recurse once, and so on -- which gives us the condition m < 3 to exit.

so, when say n=5, we start with m=5 a = f(2), then m=4, a=f(3), m=3,f=f(4),m=2,a=f(5) and it returns a.

RobotEmpire
Dec 8, 2007
edit: ^^^ This is helpful. I guess my issue is, how do you go about looking at this problem programmatically and arriving at a solution?

shrughes posted:

Evaluate the iterative solution by hand and see what it does?

Right, sorry, I did that last night:

code:
(f 5)  	= (iter 0 1 2 5) {5}
	= (iter 1 2 (2 + (2*1) + (3*0))  {4}
	= (iter 1 2 4 4)
	= (iter 2 4 (4 + (2*2) + (3*1)) {3}
	= (iter 2 4 11 3)
	= (iter 4 11 (11 + (2*4) + (3*2)) {2}
	= (iter 4 11 25 2)
	= (iter 11 25 (25 + (2*11) + (3*4)) {1}
	= (iter 11 25 (25 + 22 + 12))
	= (iter 11 25 59 1)
	= (iter 25 59 (etc...)) {0}
	= 25
I see how it's stepping through, I get the concept of how the code works, I just don't understand how to look at the initial function and arrive at this solution.

RobotEmpire fucked around with this message at 02:31 on Sep 21, 2010

raminasi
Jan 25, 2005

a last drink with no ice

RobotEmpire posted:

edit: ^^^ This is helpful. I guess my issue is, how do you go about looking at this problem programmatically and arriving at a solution?


Right, sorry, I did that last night:

code:
(f 5)  	= (iter 0 1 2 5) {5}
	= (iter 1 2 (2 + (2*1) + (3*0))  {4}
	= (iter 1 2 4 4)
	= (iter 2 4 (4 + (2*2) + (3*1)) {3}
	= (iter 2 4 11 3)
	= (iter 4 11 (11 + (2*4) + (3*2)) {2}
	= (iter 4 11 25 2)
	= (iter 11 25 (25 + (2*11) + (3*4)) {1}
	= (iter 11 25 (25 + 22 + 12))
	= (iter 11 25 59 1)
	= (iter 25 59 (etc...)) {0}
	= 25
I see how it's stepping through, I get the concept of how the code works, I just don't understand how to look at the initial function and arrive at this solution.

This problem is interesting - I taught programming to gifted kids this summer and even the top of the top struggled immensely with this particular one for some reason. Did you implement and understand the iterative Fibonacci sequence solution? It's basically just that.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

Is there a good crash-course on creating a scripting language for use inside your programs?

spiritual bypass
Feb 19, 2008

Grimey Drawer
Lua is a popular choice for adding scripting to a program written in another language.

PDP-1
Oct 12, 2004

It's a beautiful day in the neighborhood.
If you are using a .NET language, you can use the language's own compiler to read in a script file written in the same language and compile it to a usable object at run time (example here for C#). The idea is to create some base object called ScriptObject with a few virtual function hooks and then inherit from that base by compiling a file that fills in the virtual functions when your program is running.

Otherwise, Lua is popular as rt4 mentioned. I've run into problems with using it across different machines - some lua interface .dll files violate security on Vista/Win7 or cause issues with 32bit vs 64bit systems.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

I've been thinking about something for say a game, where you can have enemy AI or object behavior defined by a script. How does a script interact with your in-game variables? Do you compile it into bytecode or interpret it?

It's like a whole project in itself.

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!
In lua, you'd just use the C-lua interface to call your "behavior" functions. You can also register callback functions if you need to call some native functions from a lua function. The easiest way to get into the language is probably to read programming in lua. The standard implementation of the execution engine is a bytecode interpreter, but I think there is some JIT version too.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Java 6 has JSR-223, Scripting for the Java Platform. Out of the box, it has support for JavaScript.

thevoiceofdog
Jul 19, 2009

Terminally ambivalent.
Haven't seen too much IDL talk in this thread, but maybe someone can answer this question. I should also mention that I'm extremely inexperienced with programming in general, this class I'm taking has been my only exposure to it thus far and I don't have anywhere else to turn to for advice right now.

I need to write a function that calculates factorials (without using IDL's factorial function of course). (ie: When I input 5, it has to return the result of 5x4x3x2x1). I know this must involve a FOR loop, but I'm unsure of how to implement it to return a single value as opposed to an array, which is how I'd normally use it. And furthermore I don't know how I'm going to use said loop to multiply each number in the sequence. Can I generate a single dimensional array of those values and somehow multiply them all together with array multiplication?

I don't really have any code in particular to post, everything I've done so far is for handling instances of (n = 0) or (n < 0) where n is the value to compute the factorial of.

Any advice at all would be very appreciated.

_aaron
Jul 24, 2007
The underscore is silent.

thevoiceofdog posted:

factorial stuff
Think about how you would solve this problem on paper. Pretend you had to find the value of 10!. You probably wouldn't write out "1x2x3x4x5x6x7x8x9x10" and then solve it in one big step. You'd more likely take 1x2, then multiply that result by 3, then multiply that result by 4, etc. So you're cycling through all of the numbers and updating a running total each time. I don't want to give you code for a school assignment, but this should help you out.

SnoPuppy
Jun 15, 2005
Ruby question here:

I've been banging my head against the wall trying to solve what should be a simple problem.
I'm using ruby to try to read a text file containing strings of hex characters (like "0xcafebabe 0x1234 0xabcd") and write this to another file in binary format.

I can read the input file and turn each entry into a fixnum (using string.hex), but I can't figure out how to turn a fixnum into a byte array that I could just dump to my output file.

Any ideas?

Vanadium
Jan 8, 2005

.chr a number and it becomes a one-character string!!

RobotEmpire
Dec 8, 2007

thevoiceofdog posted:

Haven't seen too much IDL talk in this thread, but maybe someone can answer this question. I should also mention that I'm extremely inexperienced with programming in general, this class I'm taking has been my only exposure to it thus far and I don't have anywhere else to turn to for advice right now.

I need to write a function that calculates factorials (without using IDL's factorial function of course). (ie: When I input 5, it has to return the result of 5x4x3x2x1). I know this must involve a FOR loop, but I'm unsure of how to implement it to return a single value as opposed to an array, which is how I'd normally use it. And furthermore I don't know how I'm going to use said loop to multiply each number in the sequence. Can I generate a single dimensional array of those values and somehow multiply them all together with array multiplication?

I don't really have any code in particular to post, everything I've done so far is for handling instances of (n = 0) or (n < 0) where n is the value to compute the factorial of.

Any advice at all would be very appreciated.

You might want to google recursion in IDL or iteration in IDL

Not familiar with IDL at all but what I do is use "pseudo-codish" writing to figure out how to approach the problem. Let's say your input is x.

For x except 0 or 1 I need to multiply x by (x-1), then multiply that product by (x-2) until (x-n) meets my condition that x-n= 1 at which time I'll return my product to the user.

thevoiceofdog
Jul 19, 2009

Terminally ambivalent.
Thanks for the feedback guys. Fortunately I figured it out last night, and it was actually pretty simple: the PRODUCT function (which I was not aware of) will multiply all the elements in an array together and return it as one value--couldn't have a more suitable function. I just needed to use a FOR loop to create an indexed array of the values of !n.

mr_jim
Oct 30, 2006

OUT OF THE DARK

SnoPuppy posted:

Ruby question here:

I've been banging my head against the wall trying to solve what should be a simple problem.
I'm using ruby to try to read a text file containing strings of hex characters (like "0xcafebabe 0x1234 0xabcd") and write this to another file in binary format.

I can read the input file and turn each entry into a fixnum (using string.hex), but I can't figure out how to turn a fixnum into a byte array that I could just dump to my output file.

Any ideas?

Take a look at Array#pack.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

thevoiceofdog posted:

Thanks for the feedback guys. Fortunately I figured it out last night, and it was actually pretty simple: the PRODUCT function (which I was not aware of) will multiply all the elements in an array together and return it as one value--couldn't have a more suitable function. I just needed to use a FOR loop to create an indexed array of the values of !n.
Wait, hang on, what? :psyduck:

Why would you create an array and multiply all the elements together? If you're looping from 1 to n, why don't you just do the multiplication, you know, in the loop instead of building up and using an array?

thevoiceofdog
Jul 19, 2009

Terminally ambivalent.
Seemed a lot more simple to be honest. I spent a couple of hours trying to do it the way you mentioned and couldn't get the right values. I guess I just don't understand the process of loops well enough, but creating an array with values of 1 to n is like second nature to me since I usually work with image arrays. Also I'm not a very good programmer. :v:

Either way the resulting program works and I aced the assignment, so I'm happy.

Kallikrates
Jul 7, 2002
Pro Lurker
OK odd question time, I graduated with BSCS degree about 6 years ago, and swore I would never do this stuff again... I explored the world and joined the military. I've now finished that part of my life up and so now here I am trying to get a foot in the door to development. I can see myself enjoying it now that I've matured. But now I'm faced with the tasks of not being so a hot a performer in the first place (why I burned out at the end of school) being behind everyone else in terms of keeping up with trend and technologies, and being 6 years rusty.

What do you all recommend to get my head back in the game.

So far I've been tackling the MIT homework problems from their free websites and once they start being hard I might watch the lectures.

I think I never could quite get into the projects and never knew to/how to ask for help back in school so every project was a huge blow to my self esteem and just turned me off to the whole thing.

Any websites etc that you think would be beneficial would be welcome also.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Kallikrates posted:

What do you all recommend to get my head back in the game.

Think of or pick a project to do and do it. No shame in starting small and building up confidence to tackle larger projects later. Absolutely no shame in being stuck for project ideas and deciding to reimplement something just to see if you can. Don't stop working on the project until you have something you can show someone at the very least.

quote:

I think I never could quite get into the projects and never knew to/how to ask for help back in school so every project was a huge blow to my self esteem and just turned me off to the whole thing.

Well hopefully you can get into a project you start yourself, otherwise you have some deeper problems.

If you're on a project and get stuck, do a quick web search first. Then find the IRC/forum/mailing list hangout of whatever library, language, or type of project you're doing and ask questions. Or come back here. Or find someone who lives near you who knows what's up (e.g. at a local user group). Maybe check in with a former classmate. And if you're really stuck, there's always stackoverflow.

If you sort of know what you want to do for a project but are completely at a loss for how to start, come back here with some specific ideas or examples.

(A very specific problem and cure: when I was starting out I found refactoring incredibly frustrating because I hadn't heard of version control. I'd have something sort of working, then work on it only to screw it up more, but by then I'd lost what I had before. If going backwards like that sounds familiar, a quick stop by the version control thread is worth your time.)

quote:

Any websites etc that you think would be beneficial would be welcome also.

Pretend you're just getting started again and check out the CoC FAQ, feeling free to skip the incredibly basic stuff you already know. (But be honest; glancing over something and saying "I sort of remember this" isn't the same as actually being able to do it.)

RobotEmpire
Dec 8, 2007

thevoiceofdog posted:

Seemed a lot more simple to be honest. I spent a couple of hours trying to do it the way you mentioned and couldn't get the right values. I guess I just don't understand the process of loops well enough, but creating an array with values of 1 to n is like second nature to me since I usually work with image arrays. Also I'm not a very good programmer. :v:

Either way the resulting program works and I aced the assignment, so I'm happy.

I think the way I would have done it, assuming n doesn't get too large is something like:

code:
(define (foo n)
     (if (= n 1) n
         (* n (foo (- n 1)))))
No building arrays or anything like that, just simple recursion.

Adbot
ADBOT LOVES YOU

SnoPuppy
Jun 15, 2005

mr_jim posted:

Take a look at Array#pack.
Thanks!

I eventually figured out a way to do it, but it feels very clunky. Assuming that "addr" is a Fixnum, this is what I've settled on to get an array where element 0 corresponds to byte 0:
code:
[addr.to_s(16)].pack("H*").reverse 

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply