Skip to content

Instantly share code, notes, and snippets.

-- code from http://stackoverflow.com/a/21427171/849891 by András Kovács
combsOf :: Int -> [a] -> [[a]]
combsOf n _ | n < 1 = [[]]
combsOf n (x:xs) = combsOf n xs ++ map (x:) (combsOf (n - 1) xs)
combsOf _ _ = []
-- what I meant was this
change :: Int -> [Int] -> [[Int]]
change n xs
| n<0 || null xs = []
@WillNess
WillNess / APL style.hs
Last active August 29, 2015 14:04
APL style
ps = concat $ scanl (\a b-> dropWhile (<= last a) b) [2]
$ map (\(x:xs)-> x:takeWhile (< x*x) xs)
$ iterate (\(x:xs)-> minus xs [x*x, x*x+2*x..])
$ [3,5..]
ps = concat $ scanl (\a b-> dropWhile (<= last a) b) [2]
$ map (\(x:xs)-> x:takeWhile (< x*x) xs)
$ iterate (\(x:xs)-> let(a,b)=span(<x*x)xs in (x:a,minus b [x*x, x*x+2*x..]))
-- (\(x:xs)-> span (< x*x) xs |> (x:) *** (`minus` [x*x, x*x+2*x..]))
$ [3,5..]
Prelude> foldr (\x k-> k . (:) x) id [1..4] []
[4,3,2,1]
Prelude> foldl (\k x-> k . (:) x) id [1..4] []
[1,2,3,4]
Prelude>
Prelude> foldr (\x r-> (:) x r) [] [1..4]
[1,2,3,4]
Prelude> foldl (\r x-> (:) x r) [] [1..4]
[4,3,2,1]
Prelude>
-- answer at https://stackoverflow.com/questions/32937621/can-someone-explain-this-lazy-fibonacci-solution
-- by pigworker
Your "Because" is not telling the whole story. You're truncating the lists at "the story so far" and evaluating eagerly, then wondering where the rest comes from. That's not quite to grasp what's really going on, so good question.
What gets computed when you make the definition
fibs = 0 : 1 : zipWith (+) fibs (drop 1 fibs)
? Very little. Computation starts once you begin to *use* the list. Lazy computation happens only on demand.
7 ?- unionp([1,2,3,4],[3,5,2,6,7],X).
X = [1, 4, 3, 5, 2, 6, 7] ;
false.
8 ?- memberd_t(2,[1,2,3],T).
T = true.
9 ?- unionp([1,2,3,4],[3,5,2,6,7],X).
X = [1, 4, 3, 5, 2, 6, 7] ;
false.
@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
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
"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