Nicholas's posts

Post has attachment

While writing a simulation in Scala, I ran into an interesting programming exercise that was fun to implement in a functional style. I'd be interested to see how other people solve this.

Given an outcome of a uniform random variable X ~ U(0,1) and the probability mass function of a discrete random variable Y, compute a random outcome of Y.

So, you have random numbers in [0, 1), given to you by your standard library, and a mapping like ("heads" -> 1/3, "tails" -> 2/3), and you want to come up with a function to give you "heads" or "tails" with the right probabilities.

Here's my solution (in Haskell):

https://gist.github.com/nvanderw/5269152

+Ken Sheedlo +Sam Pottinger

Given an outcome of a uniform random variable X ~ U(0,1) and the probability mass function of a discrete random variable Y, compute a random outcome of Y.

So, you have random numbers in [0, 1), given to you by your standard library, and a mapping like ("heads" -> 1/3, "tails" -> 2/3), and you want to come up with a function to give you "heads" or "tails" with the right probabilities.

Here's my solution (in Haskell):

https://gist.github.com/nvanderw/5269152

+Ken Sheedlo +Sam Pottinger

Post has attachment

And then there are those days when you just write your own numbers.

Post has attachment

JavaScript with proper tail calls? I like what I'm hearing.

I'm starting to think unsubscribing from all of the default subreddits and replacing them with more insightful ones was a

**great**decision.Got this error today while writing some code on the bus. Laughed out loud. Now GHC knows how I feel! +Connor Janowiak

[1 of 1] Compiling Main ( church.hs, interpreted )

church.hs:6:31:

My brain just exploded

I can't handle pattern bindings for existential or GADT data constructors.

Instead, use a case-expression, or do-notation, to unpack the constructor.

In the pattern: Church unwrapped

In a pattern binding: Church unwrapped = church (n - 1)

In the expression:

let Church unwrapped = church (n - 1) in f . (unwrapped f)

[1 of 1] Compiling Main ( church.hs, interpreted )

church.hs:6:31:

My brain just exploded

I can't handle pattern bindings for existential or GADT data constructors.

Instead, use a case-expression, or do-notation, to unpack the constructor.

In the pattern: Church unwrapped

In a pattern binding: Church unwrapped = church (n - 1)

In the expression:

let Church unwrapped = church (n - 1) in f . (unwrapped f)

Post has attachment

I was wondering how these would interact with each other and found just the article. +Ken Sheedlo

Post has attachment

Here's my solution. I sure didn't anticipate writing this much Scheme.+Ken Sheedlo

Post has attachment

Check it out, it's a version of top that lets you monitor power usage on laptops with Intel chips. Super useful.

Linguistic nitpicking: I sometimes wish that there were a more appropriate demonym for "someone from the United States" than "American." The Spanish speaking world has "estadounidense," for instance.

Post has attachment

Nothing like learning about a language by jumping head-first into its C foreign-function interface. Static typing makes some of the runtime value representations really fun (unit, false, and the nullary list are all the same value, for instance).

Wait while more posts are being loaded