Skip to content

Instantly share code, notes, and snippets.

@WillNess
WillNess / example.txt
Created January 27, 2013 10:12 — forked from mndrix/example.txt
Lazy Fibonacci in Prolog
?- fibs(F), take(F, 20, _).
F = [0, 1, 1, 2, 3, 5, 8, 13, 21|...],
freeze(_G2395, zip_with(plus, [2584, 4181|_G2395], [4181|_G2395], _G2395)).
@WillNess
WillNess / tme.hs
Last active December 11, 2015 19:19
Dealing with unwanted sharing leads to discovery of wheels of coprimes
{-# OPTIONS_GHC -O2 -fno-cse #-}
module Main where
primesTME = 2 : gaps 3 (joinT [[p*p, p*p+2*p..] | p <- primes'])
where
primes' = 3 : gaps 5 (joinT [[p*p, p*p+2*p..] | p <- primes'])
joinT ((x:xs):t) = x : union xs (joinT (pairs t))
pairs ((x:xs):ys:t) = (x : union xs ys) : pairs t
@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)
http://stackoverflow.com/q/14713387/849891
Write a recursive function squares that takes a list of integers,
and returns a list of the squares of those integers in haskell [closed]
up vote
-7
down vote
favorite
1
@WillNess
WillNess / Bird's.hs
Created February 9, 2013 22:42
sieve of Bird
ps = (2:).minus [3..].foldr (\p r-> p*p:union [p*p+p, p*p+2*p..] r) [] $ ps
So you've got the two bugs: your
> isprimerec _ 1 = False
> isprimerec n t = if (n `rem` t) == 0 then False else isprimerec n (n-1)
should have been
> isprimerec _ 1 = True
> isprimerec n t = if (n `rem` t) == 0 then False else isprimerec n (t-1)
"Generating primes in Haskell
"I have been learning Haskell over the last few days, through Learn You A Haskell.
I've been attempting to complete some Project Euler problems, some of which require
primes. However the function I have written to try to generate some (in this case
primes below 20000) isn't outputting correctly. When I run it, GHCi returns '[1, ' and
seemingly doesn't terminate. The code I am using is:
> sieve :: (Integral a) => a -> [a] -> [a]
> sieve 20000 list = list
fmap :: (a->b) -> foo a -> foo b (a->b) -> (p->a) -> (p->b)
fmap :: (c->d) -> bar c -> bar d (c->d) -> (q->c) -> (q->d)
(.) :: (t->u) -> (s->t) -> (s->u)
(.) fmap :: -- t ~ (a->b) ; u ~ (foo a->foo b)
(s->a->b) -> (s->foo a->foo b) (s->a->b) -> (s->(p->a)->(p->b))
(.) fmap fmap :: s ~ (c->d) ; a ~ (bar c) b ~ (bar d)
( (c->d) -> foo(bar c) -> foo(bar d) )
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:
@WillNess
WillNess / gist:5019652
Created February 23, 2013 13:02
How do you define a function of signature h :: M Int -> M Int -> M Int so that h (M x) (M y) = M (x+y) without unwrapping the monad? http://stackoverflow.com/questions/15035468/how-do-you-define-a-function-of-signature-h-m-int-m-int-m-int-so-that-h
g x y = (return . (+x)) =<< y
= (=<<) (return . (+x)) y
= ((=<<) . (return .)) ((+) x) y
= (=<<) . (return .) . (+) $ x y
h mx my = mx >>= (\x ->
my >>= (\y ->
return (x+y) ))
h mx my = do x <- mx