Rule of 3


#! /usr/bin/python
statementList = [\
    "I'm very much afraid I didn't mean anything but nonsense!",
    "Just the place for a Snark!",
    "Just the place for a Snark!",
    "6 * 7 = 42",
    "I'm very much afraid I didn't mean anything but nonsense!",
    "6 * 7 = 39",
    "6 * 7 = 39",
    "Just the place for a Snark!",
    "6 * 7 = 42",
    "I'm very much afraid I didn't mean anything but nonsense!",
    "6 * 7 = 39",
    ]
for s in (s for s in frozenset(statementList) if statementList.count(s) >= 3):
    print('"' + s + '" must be true.')


(The code in the image is older than the code above the image, but looks nicer.
The background image is based on Green on Green by MJ Maccardini, 2012-26-02.)


#! /usr/bin/lua
statementlist = {
    "I'm very much afraid I didn't mean anything but nonsense!",
    "Just the place for a Snark!",
    "Just the place for a Snark!",
    "6 * 7 = 42",
    "I'm very much afraid I didn't mean anything but nonsense!",
    "6 * 7 = 39",
    "6 * 7 = 39",
    "Just the place for a Snark!",
    "6 * 7 = 42",
    "I'm very much afraid I didn't mean anything but nonsense!",
    "6 * 7 = 39",
    }
statementcount = {}
for _,statement in ipairs(statementlist) do
    local count = (statementcount[statement] or 0) + 1
    statementcount[statement] = count
    if count == 3 then print('"'..statement..'" is true!') end
end


#! /usr/bin/haskell
import Data.List
someAssertions :: [String]
someAssertions =
  [“Just the place for a Snark!”
  ,”1+1=2.”
  ,”Just the place for a Snark!”
  ,”Just the place for a Snark!”
  ,”1+1=2.”
]
atLeastThrice :: [String] -> [String]
atLeastThrice assertions =
  [head grp | grp <-
    group $ sort assertions, length grp >= 3]

Result (if loaded and executed in GHCi):

*Main> atLeastThrice someAssertions
[“Just the place for a Snark!”]

Here is an implementation of "What I tell you three times is true" in Haskell:

atLeastThrice :: [String] -> [String]
atLeastThrice assertions =
  [head grp | grp <-
    group $ sort assertions, length grp >= 3]

Thank you for providing the implementation in Haskell. Yes, your implementation looks correct. The "group" function groups the same elements in the list together, then you are filtering out the groups that have less than three elements, and finally returning the head of each group (which is the repeated assertion).

 


 
2018-08-10, update: 2024-06-28

Consent Management Platform by Real Cookie Banner