Skip to content

Instantly share code, notes, and snippets.

Replying to [comment:3 simonpj]: -- http://hackage.haskell.org/trac/ghc/ticket/7913
`nubBy` and `nub` are both intuitively ''left'' scans. `nub` should behave like
`concatMap fst . scanl (\(_,prevs) new-> if not (((elem new prevs))) then
([new],new:prevs) else ([],prevs)) ([],[])`.
`nub` should be the same as `nubBy (==)`.
Report is consistent in this regard, as it also defines `elem x = any (== x)`,
as well as
@WillNess
WillNess / redtree.hs
Last active December 18, 2015 00:19
WhyFP's `redtree` simplified
redtree f g a (Node label subtrees) = f label (redtree' f g a subtrees)
redtree' f g a ((:) subtree rest) = g (redtree f g a subtree) (redtree' f g a rest)
redtree' f g a [] = a
**IS**
redtree f g a (Node label subtrees) = f label $ reduce (g . redtree f g a) a subtrees
or
=== Dynamic dispatch mechanism of OOP ===
'''Existential types''' in conjunction with type classes can be used
to emulate the dynamic dispatch mechanism of object oriented programming
languages. To illustrate this concept I show how a classic example from
object oriented programming can be encoded in Haskell.
<haskell>
class ShapeClass a where
perimeter :: a -> Double
Q.:
Lets say I'm given two functions:
f :: [a] -> b
g :: [a] -> c
I want to write a function that is the equivalent of this:
h x = (f x, g x)
@WillNess
WillNess / gist:5755222
Last active December 18, 2015 08:39
scheme-n-queens-optimization-stragegies-sicp-chapter-2
%% http://stackoverflow.com/questions/17010561/scheme-n-queens-optimization-stragegies-sicp-chapter-2
%% also: http://ideone.com/5SZx2M
nqueens(N,Qs):- findall(I, between(1,N,I), Dom),
length( Ds,N), Ds=[Dom|DZ], append( DZ, [[]], D2s),
length( Rs,N), Rs=[[] |RZ], append( RZ, [Qs], R2s),
maplist( putOne, Ds, D2s, Rs, R2s).
%% D - available picks; R - picks made thus far
putOne(D,D2,R,[Q|R]):- select(Q,D,D2), good(Q,R).
@WillNess
WillNess / turing.hs
Last active December 18, 2015 10:29
Y, Turing and U
-- having
u x = x x -- the same piece of paper, or
v g x = x (g g x) -- !! v v x == x (v v x) !! -- -- a new piece of paper with
-- the same text on it ...
-- reduce
u u = u u = u u = ...
u v = v v = \x-> x (v v x)
u v x = v v x = x (v v x) == x (x (v v x)) == x (x (x (v v x))) == ...
== x (u v x)
== One-liners ==
primes = [n | n<-[2..], not $ elem n [j*k | j<-[2..n-1], k<-[2..n-1]]]
primes = [n | n<-[2..], not $ elem n [j*k | j<-[2..n-1], k<-[2..min j (n`div`j)]]]
primes = nubBy (((==0).).rem) [2..]
primes = [n | n<-[2..], all ((> 0).rem n) [2..n-1]]
primes = 2 : [n | n<-[3,5..], all ((> 0).rem n) [3,5..floor.sqrt$fromIntegral n]]
primes = 2 : [n | n<-[3..], all ((> 0).rem n) $ takeWhile ((<= n).(^2)) primes]
-- http://stackoverflow.com/questions/17242480/determining-ways-of-splitting-change
-- the author writes:
-- assumes the denominations are distinct. If they aren't,
-- ((there will be some dupicates present in the result produced))
change :: (Num a, Ord a) => [a] -> a -> [[a]]
change ds@ ~(d:t) sum
| sum==0 = [[]]
| null ds = []
(define (wrap x)
(cons x (lambda () () )))
(define (decdr s)
((cdr s)))
(define (app s t)
(if (null? s)
t
(cons (car s) (lambda () (app (decdr s) t)))))
@WillNess
WillNess / Turner's to Bird's.hs
Last active December 20, 2015 00:19
Turner's to Bird's
sieve (p:xs) = p : sieve [x | x <- xs, rem x p /= 0] -- (0) Turner's
ps = sieve [2..]
------------------------------------------------------------------------
ps = 2 : sieve [3..] ps
sieve xs (p:pt) | (h,t) <- span (< p*p) xs = -- (1)
h ++ sieve [x | x<-t, rem x p /= 0] pt -- Postponed Turner's
------------------------------------------------------------------------
-- rem x p /= 0 === not $ ordElem x [p,p+p..]
-- [x | x <- xs, not $ ordElem x [p,p+p..]] === diff xs [p,p+p..]
-- [x | x <- xs, (x /=).head.snd.span(< x) $ [p,p+p..]] ... -- no repeats in the