Skip to content

Instantly share code, notes, and snippets.

View MikeMKH's full-sized avatar
:shipit:
Learning about logic programming with Prolog

Mike Harris MikeMKH

:shipit:
Learning about logic programming with Prolog
  • Milwaukee, WI
  • 06:32 (UTC -05:00)
View GitHub Profile
@MikeMKH
MikeMKH / App.scala
Created August 16, 2016 11:40
Coin Changer kata in Scala with PropSpec
package com.example.coinchanger
object changer {
def apply(coins: List[Int])(amount: Int): List[Int] = {
coins.foldLeft((amount, Nil : List[Int]))((m, coin) => {
val (amount, result) = m
coin match {
case 0 => (amount, result ::: List(0))
case _ => (amount % coin, result ::: List(amount / coin))
}
@MikeMKH
MikeMKH / App.config
Created August 1, 2016 11:41
F# Local Function example with tail call "optimization"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core"
publicKeyToken="b03f5f7f11d50a3a"
culture="neutral"/>
<bindingRedirect oldVersion="4.3.1.0"
newVersion="4.4.0.0"/>
@MikeMKH
MikeMKH / Program6.cs
Last active July 28, 2016 11:40
Simple Local Function examples in C# 6 and C# 7
using static System.Console;
namespace SimpleLocalFunctionExample
{
class Program
{
private struct locals
{
public int x;
}
@MikeMKH
MikeMKH / App.scala
Created July 26, 2016 11:38
Fibonacci numbers kata in Scala (does negafibonacci)
package com.example.fibonacci
import scala.annotation.tailrec
object fibonacci {
def apply(x: Int): Int = {
@tailrec
def go(x: Int, y: Int, idx: Int): Int = idx match {
case 0 => y
case _ => go(y, x+y, idx-1)
@MikeMKH
MikeMKH / App.scala
Created July 22, 2016 11:35
Scala Local Function example with tail call "optimization"
package com.examples.factorial
import scala.annotation.tailrec
object factorial {
/**
def apply(x: Int): Int = x match {
case 0 => 1
case 1 => 1
case x => x * factorial(x-1)
@MikeMKH
MikeMKH / Program.cs
Created July 14, 2016 12:52
C# 7 is example
using System;
using System.Collections.Generic;
// Visual Studio 15 Preview 3
// Conditional compilation symbols: __DEMO__ ,__DEMO_EXPERIMENTAL__
namespace PatternExamples
{
class Program
{
static void Main(string[] args)
@MikeMKH
MikeMKH / Program.cs
Created July 13, 2016 13:23
C# 4 "Local Function" example with tuples in a tail call
using System;
namespace TupleExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"1! = {Factorial(1)}");
Console.WriteLine($"5! = {Factorial(5)}");
@MikeMKH
MikeMKH / Program.cs
Last active July 14, 2016 00:18
Precondition factorial example in C# 7
using System;
// Visual Studio 15 Preview 2
// Conditional compilation symbols: __DEMO__ ,__DEMO_EXPERIMENTAL__
namespace TailCallExamples
{
class Program
{
static void Main(string[] args)
{
@MikeMKH
MikeMKH / factorial.clj
Created July 13, 2016 11:28
Factorial example in Clojure **note will cause StackOverflow**
(ns com.blogspot.comp-phil.factorial)
;; Causes StackOverflow
(defn factorial [n]
{:pre [((comp not neg?) n)]
:post [((comp not neg?) %)]}
(if (= n 0)
1
(* n (factorial (dec n)))))
@MikeMKH
MikeMKH / application.e
Created July 13, 2016 02:33
Factorial example in Eiffel
note
description : "example of factorial"
class
APPLICATION
create
make
feature -- Initialization