Skip to content

Instantly share code, notes, and snippets.

@olliefr
Created May 21, 2014 12:07
Show Gist options
  • Save olliefr/d6312d8195e9a30aa80c to your computer and use it in GitHub Desktop.
Save olliefr/d6312d8195e9a30aa80c to your computer and use it in GitHub Desktop.
Random.self_init is not random for me
open Core.Std
let _ = Random.self_init
(*
FIXME there must be something in the standard library to do this!
Iterate a function over a value, tail-recursively.
n: how many times
f: function to apply
a: initial value of the argument
*)
let rec iterate n f a =
if n<=0
then a
else iterate (n-1) f (f a)
(* Wandering Light *)
let light = (0,0)
let wander (x,y) =
match (1 + Random.int 4) with
1 -> (x+1, y)
| 2 -> (x, y+1)
| 3 -> (x-1, y)
| 4 -> (x, y+1)
| _ -> failwith "random direction is not 1 to 4, wtf?"
let render (x,y) = printf "%i %i\n" x y
let step light =
let newlight = wander light in
render newlight;
newlight
let _ = iterate 5 step (0,0)
(* THE END *)
OCaml compiler:
The OCaml toplevel, version 4.01.0
OPAM output:
Installed packages for system:
async 111.13.00 Monadic concurrency library
async_extra 111.13.00 Monadic concurrency library
async_graphics 0.5.1 Async wrapper for the OCaml Graphics library
async_kernel 111.11.00 Monadic concurrency library
async_unix 111.13.00 Monadic concurrency library
base-bigarray base Bigarray library distributed with the OCaml compiler
base-threads base Threads library distributed with the OCaml compiler
base-unix base Unix library distributed with the OCaml compiler
bin_prot 111.03.00 A binary protocol generator
biniou 1.0.9 Binary data format designed for speed, safety, ease of use and backward compatibility as protocols evolve
camlp4 4.01.0 Camlp4 is a system for writing extensible parsers for programming languages
camomile 0.8.5 A comprehensive Unicode library
cohttp 0.11.2 HTTP library for Lwt, Async and Mirage
comparelib 109.60.00 Part of Jane Street’s Core library
conduit 0.5.0 Network connection library for TCP and SSL
core 111.13.00 Industrial strength alternative to OCaml's standard library
core_bench 109.58.01 Benchmarking library
core_extended 111.13.00 Extra components that are not as closely vetted or as stable as Core
core_kernel 111.13.00 Industrial strength alternative to OCaml's standard library
cppo 0.9.3 Equivalent of the C preprocessor for OCaml programs
cryptokit 1.9 Cryptographic primitives library.
custom_printf 111.03.00 Extension for printf format strings
easy-format 1.0.2 High-level and functional interface to the Format module of the OCaml standard library
enumerate 111.08.00 Quotation expanders for enumerating finite types.
fieldslib 109.20.03 Syntax extension to define first class values representing record fields, to get and set record fields, iterate and fold over all fields of a record and create new record values
herelib 109.35.02 Part of Jane Street’s Core library
lambda-term 1.6 Terminal manipulation library for OCaml
lwt 2.4.5 A cooperative threads library for OCaml
menhir 20130912 LR(1) parser generator
ocamlfind 1.4.0 A library manager for OCaml
ounit 2.0.0 Unit testing framework loosely based on HUnit. It is similar to JUnit, and other XUnit testing frameworks
pa_bench 109.55.02 Syntax extension for inline benchmarks
pa_ounit 109.53.02 Syntax extension for oUnit
pa_test 111.08.00 Quotation expander for assertions.
pipebang 110.01.00 Part of Jane Street’s Core library
re 1.2.2 RE is a regular expression library for OCaml
re2 111.08.00 OCaml bindings for RE2, Google's regular expression library
react 1.1.0 Declarative events and signals for OCaml
sexplib 111.13.00 Library for serializing OCaml values to and from S-expressions
ssl 0.4.7 Bindings for OpenSSL
stringext 0.0.1 Extra string functions for OCaml
textutils 111.06.00 Text output utilities
type_conv 111.13.00 Library for building type-driven syntax extensions
typerep 111.06.00 typerep is a library for runtime types.
uri 1.6.0 RFC3986 URI parsing library
utop 1.12 Universal toplevel for OCaml
variantslib 109.15.03 Part of Jane Street’s Core library
yojson 1.1.8 Yojson is an optimized parsing and printing library for the JSON format
zed 1.3 Abstract engine for text edition in OCaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment