Roll The Dice- /r/DailyProgrammer 130 Easy

So, one thing I do to practice programming is doing some exercises at Reddit’s Daily Programmer subreddit.  I thought I’d work through the process on one of them.

This challenge, , is to write a program that will take a string of the form NdM, where N is a number of dice, and M is the number of faces on said dice, and then produce a list of rolling an M die N times.

Read more of this post



So, I just discovered the Unix tool bc.  Given that I’m a Mac guy as much for the Unix as for anything else, this got me interested.

bc is a mathematical programming language and interactive calculator.  The latter is useful, sometimes I need a calculator, and for what I’m doing that instant, a terminal app is most appropriate.  bc fills that role well.

bc also is a C like programming language for mathematical operations.  I haven’t gotten deeply into it, but here’s a factorial function, with a simple recursive implementation.  

/*Factorial function in bc*/

define fact(x)


    if( x <= 1){

        return 1


        return x * fact(x-1)



So. SML lets you redefine basic operators on basic types

For instance, I could do this-

fun op-(x,y) = x+y

This makes the ‘-’ operator do integer addition.  Reals?  Hah, no subtracting them now!  Fails type checking on anything but ints.

I can also do:

fun op-(x) = ~x;

Now, the ‘-’ operator does nothing except spawn error messages wherever it is used.  Well, wherever it is used except the definition of that function.  Which type checks perfectly fine.  Uses of this version of ‘-’ will either fail type checking, throw up syntax errors, or both.  I’m mystified as to why this function definition actually type checks, when it it guaranteed to throw up errors when used.

Redefining how operators behave can be useful, but when it can *only* be an infix operator, why allow me to redefine it to take only one argument?  I discovered this mostly by accident reading library documentation and saying “hey, I wonder if this would work…”, so maybe there’s some additional syntax that can convert it to prefix or postfix?  Even so, why allow the infix version to be defined to take one argument, guaranteeing a noncallable function?

Functional Programming- Currying

Some confusing stuff, but I’m starting to see the potential.  

Currying is a technique that lets you define a function taking multiple arguments as a series of functions that take one argument, and returns a function that takes the rest.  You can define it all the way to the last argument.

This lets you call a funciton with fewer arguments than it takes, and fill in the extras later.  For instance, in SML, assuming powint takes two ints and returns the first int to the power of the second:

val p = powint 2

This makes p a function taking one argument, and giving you back the corresponding power of two.  Oh, you want powers of 10?

val p10 = powint 10

Next call, you have to do 3^5?  You can call it with both arguments right away if you want.

val p3_5 = powint 3 5

I’m still a bit confused on working with this, but the potential is very nice.

Perfect numbers in Python

Always test functions before writing others that depend on them

Yeah.  Didn’t do that.  Now I have half a dozen functions, 50 or so lines of code, I have no idea if it works or not.  

At least I’ve traced this function as for sure not working.  And the other functions that it depends on are tested and work correctly.  So even if the rest are completely wrong, at least I’ve found a good starting point for debugging.

SML is odd

(if exp1 then exp2 else exp3) div (if exp4 then exp5 else exp6)

Is valid and for at least some problems, is entirely reasonable.

I think the key to SML, at least so far… *everything* is an expression.  Any complete statement returns a value, and anything that returns a value counts as an expression.  Ok, small exceptions here and there like, well, exceptions, but approaching it with “everything is an expression” has really helped my understanding.