Skip to content

Instantly share code, notes, and snippets.

import math
import sys # stackoverflow.com/questions/49149932/
# python-generator-and-setgenerator-get-different-results
# by stackoverflow.com/users/4658633/blaise-wang
import time
from mpi4py import MPI
import eratosthenes
@WillNess
WillNess / mcc.erl
Last active February 28, 2018 00:31
apply f args = eval [f, ...[[QUOTE, a] | a <- args]...] [] % McCarthy-original-LISP-paper
eval e a | atom e = case [v | [n, v] <- a, n == e] of {[v, ...] -> v} % first matching name's value
| otherwise =
case e of
[QUOTE, x] -> x % "expression"
[FUNCTION, x] -> [FUNARG, x, a] % closure (new)
[ATOM, x] -> atom (eval x a)
[EQ, x, y] -> eval x a == eval y a
[CONS, x, y] -> [eval x a, ...eval y a...]

OK, so here's a short progression of implementations for comparison, for clarity:

 {-# LANGUAGE ViewPatterns #-}
 import Data.Array.Unboxed
 import Data.List (tails, inits)
 import qualified Data.List.Ordered as O     -- package 'data-ordlist'

(\) = O.minus

@WillNess
WillNess / foldr-insert.hs
Last active October 22, 2017 04:41
foldr - insert - paramorphism
http://stackoverflow.com/questions/20568276/implement-insert-in-haskell-with-foldr
/20570385#20570385
----
You need a [paramorphism](http://stackoverflow.com/a/13317563/849891) for that:
para :: (a -> [a] -> b -> b) -> b -> [a] -> b
foldr :: (a -> b -> b) -> b -> [a] -> b
para c n (x : xs) = c x xs (para c n xs)
my edit for
https://stackoverflow.com/questions/45225142/the-pattern-with-functions-like-bool-either-etc/45238164#45238164
by https://stackoverflow.com/users/3234959/chi
First, the types you mention are not really GADTs, they are plain ADTs, since the return type of each constructor is always `T a`. A proper GADT would be something like
data T a where
K1 :: T Bool -- not T a
@WillNess
WillNess / ChurchList.hs
Last active June 7, 2017 20:44
Church-encoded Lists are efficient?
http://stackoverflow.com/questions/15589556/why-are-difference-lists-not-an-instance-of-foldable
/15593349?noredirect=1#comment22277557_15593349
a = singleton 1 singleton x = ChurchList $ \k z -> k x z
b = snoc a 2 snoc xs x = ChurchList $ \k z -> runList xs k (k x z)
c = snoc b 3
d = append c c append u v = ChurchList $ \k z -> runList u k (runList v k z)
runList a k z = k 1 z a := ChurchList $ \k z -> k 1 z
runList b k z = runList a k (k 2 z) = k 1 (k 2 z) b := ChurchList $ \k z -> runList a k (k 2 z)
B is simply defined as
B f g x = f (g x)
When we supply it with two args,
it still needs the third. (B f g) needs one more argument.
Let's see what is B B B. It supplies two args to the first B,
so we need to add the third there:
-- correction to answer http://stackoverflow.com/a/4787707/849891
-- answered Jan 24 '11 at 22:00 by Thomas M. DuBuisson
You need a way for the fixpoint to terminate. Expanding your example, it's obvious it won't finish:
fix id
--> let x = id x in x
--> let x = x in x
--> let x = x in x
--> ...
@WillNess
WillNess / quicksort3.hs
Last active February 10, 2016 13:49
quicksort3
quicksort3 xs = go xs [] where
go (x:xs) zs = part x xs zs [] [] []
go [] zs = zs
part x [] zs a b c = go a ((x : b) ++ go c zs)
part x (y:ys) zs a b c =
case compare y x of
LT -> part x ys zs (y:a) b c
EQ -> part x ys zs a (y:b) c
GT -> part x ys zs a b (y:c)
@WillNess
WillNess / where-to-start.hs
Last active January 1, 2016 23:29
to start from the start, or from squares in bounded trial divisions sieve, does it change the complexity?
-- http://stackoverflow.com/a/1510186/849891
-- does it change the complexity, where to start from ....? -- make an empirical observation:
{-# OPTIONS_GHC -O2 #-}
g n = let {
svs = takeWhile ((< n).(^2).head) $ sv [2..];
ps = map head svs;
qs = map (^2) ps;