Skip to content

Instantly share code, notes, and snippets.

View tmountain's full-sized avatar

Travis Whitton tmountain

View GitHub Profile
module Set
( findSets
, Shape (..)
, Color (..)
, Shade (..)
, Card (..)
, Count (..)
) where
import Data.List (nub, sort, tails)
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
module Main where
import Data.Char (isSpace)
import qualified Data.List as D
import qualified Data.List.Split as S
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
import qualified Text.Read as TR
data Score = Score
almostFactorial = (\f -> (\n -> if n == 0 then 1 else n * f (n - 1)))
facA = almostFactorial facA
facA 3 -- returns 6
(ns ycomb.core
(:gen-class))
(def almost-factorial
(fn [f]
(fn [n]
(if (= n 0)
1
(* n (f (- n 1)))))))
(defn A*
"Finds a path between start and goal inside the graph described by edges
(a map of edge to distance); estimate is an heuristic for the actual
distance. Accepts a named option: :monotonic (default to true).
Returns the path if found or nil."
[edges estimate start goal & {mono :monotonic :or {mono true}}]
(let [f (memoize #(estimate % goal)) ; unsure the memoization is worthy
neighbours (reduce (fn [m [a b]] (assoc m a (conj (m a #{}) b)))
{} (keys edges))]
(loop [q (priority-map start (f start))
(defn A*
"Finds a path between start and goal inside the graph described by edges
(a map of edge to distance); estimate is an heuristic for the actual
distance. Accepts a named option: :monotonic (default to true).
Returns the path if found or nil."
[edges estimate start goal & {mono :monotonic :or {mono true}}]
(let [f (memoize #(estimate % goal)) ; unsure the memoization is worthy
neighbours (reduce (fn [m [a b]] (assoc m a (conj (m a #{}) b)))
{} (keys edges))]
(loop [q (priority-map start (f start))
;; Three very fundamental operators in any sort of programming are map, filter
;; and reduce.
;; They represent the common programming tasks of transforming a collection,
;; selecting from it, and summing over it.
;; Most people think that map and filter are fairly obvious, but there seems to
;; be a certain amount of confusion about reduce.
;; But it's actually very simple in concept, and represents an easy idea.
-- problem 1
sum [ x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0 ]
-- problem 2
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
sum [ x | x <- takeWhile (<= 4*10^6) $ drop 2 fibs, even ]
-- problem 3
prime n
| n < 2 = False
(defn epoch
[]
(quot (System/currentTimeMillis) 1000))
(let [cache (atom {})]
(defn add-key
[k v expire]
(swap! cache assoc k {:v v, :e expire, :c (epoch)}))
(defn del-key