This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
?- 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)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ps = (2:).minus [3..].foldr (\p r-> p*p:union [p*p+p, p*p+2*p..] r) [] $ ps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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 |
OlderNewer