Skip to main content
added 202 characters in body
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

The first observation that I would make is that your type signature is too specific:

findSame :: [Int] -> [Int] -> [Int] -> [Int]

The function should work for any Ord a:

findSame :: Ord a => [a] -> [a] -> [a] -> [a]

The second observation is that you don't need to work on three lists at a time. You could just take two lists at a time:

findSame tris $ findSame pents hexes

But what is the findSame operation then? It's the intersection of two ordered lists. A quick search reveals an isect function in Data.Ordered.List.

So, the solution could just be

import Data.List.Ordered (isect)
head $ filter (> 40755) $ isect tris $ isect pents hexes

Or, more elegantly:

head $ filter (> 40755) $ foldr1 isect [hexes, pents, tris]

The implementation of isect is simple enough that you could just "steal" it.


Minor note: Personally, I would define the sequences so that the subscripts match the notation in the problem statement.

tris  = scanl (+) 0 [1..]
pents = scanl (+) 0 [1, 4..]
hexes = scanl (+) 0 [1, 5..]

That also makes it easier to generalize:

polygonalSeq :: Int -> [Int]
polygonalSeq s = scanl (+) 0 [1, (s - 1)..]
head $ filter (> 40755) $ foldr1 isect $ map polygonalSeq [6, 5, 3]

The first observation that I would make is that your type signature is too specific:

findSame :: [Int] -> [Int] -> [Int] -> [Int]

The function should work for any Ord a:

findSame :: Ord a => [a] -> [a] -> [a] -> [a]

The second observation is that you don't need to work on three lists at a time. You could just take two lists at a time:

findSame tris $ findSame pents hexes

But what is the findSame operation then? It's the intersection of two ordered lists. A quick search reveals an isect function in Data.Ordered.List.

So, the solution could just be

import Data.List.Ordered (isect)
head $ filter (> 40755) $ isect tris $ isect pents hexes

Or, more elegantly:

head $ filter (> 40755) $ foldr1 isect [hexes, pents, tris]

The implementation of isect is simple enough that you could just "steal" it.


Minor note: Personally, I would define the sequences so that the subscripts match the notation in the problem statement.

tris  = scanl (+) 0 [1..]
pents = scanl (+) 0 [1, 4..]
hexes = scanl (+) 0 [1, 5..]

The first observation that I would make is that your type signature is too specific:

findSame :: [Int] -> [Int] -> [Int] -> [Int]

The function should work for any Ord a:

findSame :: Ord a => [a] -> [a] -> [a] -> [a]

The second observation is that you don't need to work on three lists at a time. You could just take two lists at a time:

findSame tris $ findSame pents hexes

But what is the findSame operation then? It's the intersection of two ordered lists. A quick search reveals an isect function in Data.Ordered.List.

So, the solution could just be

import Data.List.Ordered (isect)
head $ filter (> 40755) $ isect tris $ isect pents hexes

Or, more elegantly:

head $ filter (> 40755) $ foldr1 isect [hexes, pents, tris]

The implementation of isect is simple enough that you could just "steal" it.


Minor note: Personally, I would define the sequences so that the subscripts match the notation in the problem statement.

tris  = scanl (+) 0 [1..]
pents = scanl (+) 0 [1, 4..]
hexes = scanl (+) 0 [1, 5..]

That also makes it easier to generalize:

polygonalSeq :: Int -> [Int]
polygonalSeq s = scanl (+) 0 [1, (s - 1)..]
head $ filter (> 40755) $ foldr1 isect $ map polygonalSeq [6, 5, 3]
added 90 characters in body
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

The first observation that I would make is that your type signature is too specific:

findSame :: [Int] -> [Int] -> [Int] -> [Int]

The function should work for any Ord a:

findSame :: Ord a => [a] -> [a] -> [a] -> [a]

The second observation is that you don't need to work on three lists at a time. You could just take two lists at a time:

findSame tris $ findSame pents hexes

But what is the findSame operation then? It's the intersection of two ordered lists. A quick search reveals an isect function in Data.Ordered.List.

So, the solution could just be

import Data.List.Ordered (isect)
head $ filter (> 40755) $ isect tris $ isect pents hexes

Or, more elegantly:

head $ filter (> 40755) $ foldr1 isect [hexes, pents, tris]

The implementation of isect is simple enough that you could just "steal" it.


Minor note: Personally, I would define the sequences so that the subscripts match the notation in the problem statement.

tris  = scanl (+) 0 [1..]
pents = scanl (+) 0 [1, 4..]
hexes = scanl (+) 0 [1, 5..]

The first observation that I would make is that your type signature is too specific:

findSame :: [Int] -> [Int] -> [Int] -> [Int]

The function should work for any Ord a:

findSame :: Ord a => [a] -> [a] -> [a] -> [a]

The second observation is that you don't need to work on three lists at a time. You could just take two lists at a time:

findSame tris $ findSame pents hexes

But what is the findSame operation then? It's the intersection of two ordered lists. A quick search reveals an isect function in Data.Ordered.List.

So, the solution could just be

import Data.List.Ordered (isect)
head $ filter (> 40755) $ isect tris $ isect pents hexes

The implementation of isect is simple enough that you could just "steal" it.


Minor note: Personally, I would define the sequences so that the subscripts match the notation in the problem statement.

tris  = scanl (+) 0 [1..]
pents = scanl (+) 0 [1, 4..]
hexes = scanl (+) 0 [1, 5..]

The first observation that I would make is that your type signature is too specific:

findSame :: [Int] -> [Int] -> [Int] -> [Int]

The function should work for any Ord a:

findSame :: Ord a => [a] -> [a] -> [a] -> [a]

The second observation is that you don't need to work on three lists at a time. You could just take two lists at a time:

findSame tris $ findSame pents hexes

But what is the findSame operation then? It's the intersection of two ordered lists. A quick search reveals an isect function in Data.Ordered.List.

So, the solution could just be

import Data.List.Ordered (isect)
head $ filter (> 40755) $ isect tris $ isect pents hexes

Or, more elegantly:

head $ filter (> 40755) $ foldr1 isect [hexes, pents, tris]

The implementation of isect is simple enough that you could just "steal" it.


Minor note: Personally, I would define the sequences so that the subscripts match the notation in the problem statement.

tris  = scanl (+) 0 [1..]
pents = scanl (+) 0 [1, 4..]
hexes = scanl (+) 0 [1, 5..]
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

The first observation that I would make is that your type signature is too specific:

findSame :: [Int] -> [Int] -> [Int] -> [Int]

The function should work for any Ord a:

findSame :: Ord a => [a] -> [a] -> [a] -> [a]

The second observation is that you don't need to work on three lists at a time. You could just take two lists at a time:

findSame tris $ findSame pents hexes

But what is the findSame operation then? It's the intersection of two ordered lists. A quick search reveals an isect function in Data.Ordered.List.

So, the solution could just be

import Data.List.Ordered (isect)
head $ filter (> 40755) $ isect tris $ isect pents hexes

The implementation of isect is simple enough that you could just "steal" it.


Minor note: Personally, I would define the sequences so that the subscripts match the notation in the problem statement.

tris  = scanl (+) 0 [1..]
pents = scanl (+) 0 [1, 4..]
hexes = scanl (+) 0 [1, 5..]