Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Brewfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ cask "dotnet-sdk"
brew "julia"
brew "haskell-stack"
brew "kotlin"
cask "kotlin-native"
cask "kotlin-native"
tap "hook-lang/hook"
brew "hook"
12 changes: 12 additions & 0 deletions fibonacci/hook/code.hk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn fibonacci(n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n-1) + fibonacci(n-2);
}

let u = to_int(args[0]);
var r = 0;
for (var i = 1; i < u; i++) {
r += fibonacci(i);
}
println(r);
1 change: 1 addition & 0 deletions hello-world/hook/code.hk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
println("Hello, World!");
73 changes: 73 additions & 0 deletions levenshtein/hook/code.hk
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { new_array, fill } from arrays;

// Can either define your own min function
// or use a language / standard library function
fn min(a, b, c) {
var min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;
}

fn levenshtein_distance(str1t, str2t) {
// Get lengths of both strings
let mt = len(str1t);
let nt = len(str2t);
// Assign shorter one to str1, longer one to str2
let str1 = if (mt <= nt) str1t else str2t;
let str2 = if (mt <= nt) str2t else str1t;
// store the lengths of shorter in m, longer in n
let m = if (str1 == str1t) mt else nt;
let n = if (str1 == str1t) nt else mt;

// Create two rows, previous and current
var prev = new_array(m+1);
var curr = fill(0, m+1);

// initialize the previous row
for (var i = 0; i <= m; i++) {
prev[] = i;
}

// Iterate and compute distance
for (var i = 1; i <= n; i++) {
curr[0] = i;
for (var j = 1; j <= m; j++) {
let cost = if (str1[j-1] == str2[i-1]) 0 else 1;
curr[j] = min(
prev[j] + 1, // Deletion
curr[j-1] + 1, // Insertion
prev[j-1] + cost // Substitution
);
}
for (var j = 0; j <= m; j++) {
prev[j] = curr[j];
}
}

// Return final distance, stored in prev[m]
return prev[m];
}

var min_distance = -1;
var times = 0;
// Iterate through all combinations of command line args
let argc = len(args);
for (var i = 0; i < argc; i++) {
for (var j = 0; j < argc; j++) {
// Don't compare the same string to itself
if (i != j) {
let distance = levenshtein_distance(args[i], args[j]);
if (min_distance == -1 || min_distance > distance) {
min_distance = distance;
}
times++;
}
}
}

// The only output from the program should be the times (number of comparisons)
// and min distance calculated of all comparisons. Two total lines of output,
// formatted exactly like this.
println("times: " + to_string(times));
println("min_distance: " + to_string(min_distance));
14 changes: 14 additions & 0 deletions loops/hook/code.hk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { floor } from math;
import { rand } from numbers;
import { fill } from arrays;

let u = to_int(args[0]); // Get an input number from the command line
var r = floor(rand() * 10000); // Get a random number 0 <= r < 10k
var a = fill(0, 10000); // Array of 10k elements initialized to 0
for (var i = 0; i < 10000; i++) { // 10k outer loop iterations
for (var j = 0; j < 100000; j++) { // 100k inner loop iterations, per outer loop iteration
a[i] = a[i] + j%u; // Simple sum
}
a[i] += r; // Add a random value to each element in array
}
println(a[r]); // Print out a single element from the array
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ run "Free Pascal" "./fpc/code" "./fpc/code" "${input}"
run "Go" "./go/code" "./go/code" "${input}"
run "Haskell" "./haskell/code" "./haskell/code" "${input}"
#run "Haxe JVM" "haxe/code.jar" "java -jar haxe/code.jar" "${input}" # was getting errors running `haxelib install hxjava`
run "Hook" "./hook/code.hk" "hook ./hook/code.hk" "${input}"
run "Inko" "./inko/code" "./inko/code" "${input}"
run "Java" "./jvm/code.class" "java jvm.code" "${input}"
#run "Java Native" "./jvm.code" "./jvm.code" "${input}"
Expand Down