Top-Rated Free Essay

Nuclear Weapons

Better Essays
Topics: Gender, Female, Male, Sex, Gamete, Logic
Functional programming
Ex.
C++. #include <iostream> // Fibonacci numbers, imperative style int fibonacci(int iterations) { int first = 0, second = 1; // seed values for (int i = 0; i < iterations; ++i) { int sum = first + second; first = second; second = sum; } return first; } int main() { std::cout << fibonacci(10) << "\n"; return 0; }

A functional version (in Haskell) has a different feel to it: -- Fibonacci numbers, functional style -- describe an infinite list based on the recurrence relation for Fibonacci numbers fibRecurrence first second = first : fibRecurrence second (first + second) -- describe fibonacci list as fibRecurrence with initial values 0 and 1 fibonacci = fibRecurrence 0 1 -- describe action to print the 10th element of the fibonacci list main = print (fibonacci !!

Logic Programming ------------------------------------------------- {-
-------------------------------------------------
Problem:
-------------------------------------------------

------------------------------------------------- There is a tribe where all the Male members speak true statements and Female
-------------------------------------------------
members never speak two true statements in a row, nor two untrue statements in
-------------------------------------------------
a row. (I apologize for the obvious misogyny).
-------------------------------------------------

------------------------------------------------- A researcher comes across a mother, a father, and their child. The mother and
-------------------------------------------------
father speak English but the child does not. However, the researcher asks the
-------------------------------------------------
child "Are you a boy?". The child responds but the researcher doesn't
-------------------------------------------------
understand the response and turns to the parents for a translation.
-------------------------------------------------

------------------------------------------------- Parent 1: "The child said 'I am a boy.'"
-------------------------------------------------
Parent 2: "The child is a girl. The child lied."
-------------------------------------------------

------------------------------------------------- What is the sex of parent 1, parent 2, the child, and what sex did the child
-------------------------------------------------
say they were?
-------------------------------------------------

------------------------------------------------- Bonus:
-------------------------------------------------

------------------------------------------------- There is a unique solution for heterosexual, gay, and lesbian couples. Find
-------------------------------------------------
all three solutions.
-------------------------------------------------

------------------------------------------------- Solution:
-------------------------------------------------

------------------------------------------------- Run the code :)
-------------------------------------------------

------------------------------------------------- Approach:
-------------------------------------------------

------------------------------------------------- Use the monadic properties of lists to setup some basic logic programming.
-------------------------------------------------
There are four variables in the puzzle: Sex of parent 1, Sex of parent 2, Sex
-------------------------------------------------
of the child, and the Sex the child said they were. Each of these has two
-------------------------------------------------
possibilities, which means we've got 2^4 == 16 possible outcomes.
-------------------------------------------------

------------------------------------------------- Using List Monads we can realize all 2^4 outcomes in a straightforward
-------------------------------------------------
fashion. Then it is just a matter of testing each combination to make sure it
-------------------------------------------------
fits the constraints of the puzzle.
-------------------------------------------------

------------------------------------------------- We have two axioms:
-------------------------------------------------

------------------------------------------------- 1. A Male does not lie.
-------------------------------------------------
2. A Female will never tell two lies or two truths in a row.
-------------------------------------------------

------------------------------------------------- And we have three statements (i.e. logical expressions) in the puzzle:
-------------------------------------------------

------------------------------------------------- 1. The child said a single statement, in which they declared their sex.
-------------------------------------------------
2. Parent 1 said a single statement: "The child said 'I am a a boy'"
-------------------------------------------------
3. Parent 2 said two statements: "The child is a girl. The child lied."
-------------------------------------------------

------------------------------------------------- Each of those three statements is realized as a function. These functions do
-------------------------------------------------
not test the truth of the statement but rather test its logical validity in
-------------------------------------------------
the face of the axioms.
-------------------------------------------------

------------------------------------------------- For example, if the Child is Male then it is not possible the child said they
-------------------------------------------------
were Female since that would violate axiom 1. Similarly if the Child is Female
-------------------------------------------------
then no matter if they lied or told the truth the statement is valid in the
-------------------------------------------------
face of the axioms, this is an example of the truth of statement differing
-------------------------------------------------
from its logical validity.
-------------------------------------------------

------------------------------------------------- -}
-------------------------------------------------

------------------------------------------------- -- People are either Male or Female, this represents the constraints of the puzzle.
-------------------------------------------------
data Sex = Male | Female deriving (Eq, Show)
-------------------------------------------------

------------------------------------------------- -- When creating an answer we stuff it into this data structure
-------------------------------------------------
data PuzzleAnswer = PuzzleAnswer {
-------------------------------------------------
parent1 :: Sex,
-------------------------------------------------
parent2 :: Sex,
-------------------------------------------------
child :: Sex,
-------------------------------------------------
child_desc :: Sex
-------------------------------------------------
}
-------------------------------------------------

------------------------------------------------- -- This lets us print out the data structure in a friendly way
-------------------------------------------------
instance Show (PuzzleAnswer) where
-------------------------------------------------
show pa = "Parent1 is " ++ (show $ parent1 pa) ++ "\n" ++
-------------------------------------------------
"Parent2 is " ++ (show $ parent2 pa) ++ "\n" ++
-------------------------------------------------
"The child is " ++ (show $ child pa) ++ "\n" ++
-------------------------------------------------
"The child said they were " ++ (show $ child_desc pa) ++ "\n"
-------------------------------------------------

------------------------------------------------- {-
-------------------------------------------------
childs_statement_is_valid(child_sex, child_described_sex)
-------------------------------------------------

------------------------------------------------- The only combination that violates the axioms is (Male, Female) since a Male
-------------------------------------------------
does not lie. Obviously (Male, Male) and (Female, *) are valid statements.
-------------------------------------------------
-}
-------------------------------------------------
childs_statement_is_valid :: Sex -> Sex -> Bool
-------------------------------------------------
childs_statement_is_valid Male Female = False
-------------------------------------------------
childs_statement_is_valid _ _ = True
-------------------------------------------------

------------------------------------------------- {-
-------------------------------------------------
parent1_statement_is_valid(parent1_sex, child_described_sex)
-------------------------------------------------

------------------------------------------------- Parent 1 said "The child said 'I am a boy'". The only invalid combination is
-------------------------------------------------
(Male, Female), because that'd imply a Male (the parent) lied. Obviously
-------------------------------------------------
(Male, Male) is okay because then parent 1 is telling the truth. (Female, *)
-------------------------------------------------
is dubious because you can't trust a Female.
-------------------------------------------------
-}
-------------------------------------------------
parent1_statement_is_valid :: Sex -> Sex -> Bool
-------------------------------------------------
parent1_statement_is_valid Male Female = False
-------------------------------------------------
parent1_statement_is_valid _ _ = True
-------------------------------------------------

------------------------------------------------- {-
-------------------------------------------------
parent2_statement_is_valid(parent1_sex, child_sex, child_described_sex)
-------------------------------------------------

------------------------------------------------- Parent 2 said "The child is a girl. The child lied." If Parent 2 is Male
-------------------------------------------------
then the only way this can be a legal statement is if the chlid is Female and
-------------------------------------------------
said they were Male. This would mean the child is in fact a girl and the
-------------------------------------------------
child did in fact lie, two statements which are both true. This corresponds
-------------------------------------------------
to (Male, Female, Male) being legal.
-------------------------------------------------

------------------------------------------------- If Parent2 is Female then (Female, *, Female) are both true. (Female, Male,
-------------------------------------------------
Female) is true because the first statement is false (the child is a girl) but
-------------------------------------------------
the second one is true (the child lied -- it said Female when it was Male).
-------------------------------------------------
(Female, Female, Female) is also legal since the first statement (the child is
-------------------------------------------------
a girl) is true but the second one is a lie (the child lied -- the child said
-------------------------------------------------
they were Female and they are Female).
-------------------------------------------------

------------------------------------------------- Any other combination will be illegal.
-------------------------------------------------
-}
-------------------------------------------------
parent2_statement_is_valid :: Sex -> Sex -> Sex -> Bool
-------------------------------------------------
parent2_statement_is_valid Male Female Male = True
-------------------------------------------------
parent2_statement_is_valid Female _ Female = True
-------------------------------------------------
parent2_statement_is_valid _ _ _ = False
-------------------------------------------------

------------------------------------------------- {-
-------------------------------------------------
Here we use the List Monad to declare the four variables, each ranging over
-------------------------------------------------
the set [Male, Female]. The List Monad transparently constructs all 2^4
-------------------------------------------------
possibilities. The guard statements discard statements that are invalid. We
-------------------------------------------------
have four guards, the three described above and an additional guard that
-------------------------------------------------
asserts the parents are not the same sex.
-------------------------------------------------

------------------------------------------------- The result is a list of tuples listing all possible solutions. There happens
-------------------------------------------------
to be only one, if there was more than one than the other legal ones would be
-------------------------------------------------
returned too.
-------------------------------------------------
-}
-------------------------------------------------
solve_puzzle :: (Sex -> Sex -> Bool) -> [PuzzleAnswer]
-------------------------------------------------
solve_puzzle sexuality_pred = do
-------------------------------------------------
parent1 <- [Male, Female]
-------------------------------------------------
parent2 <- [Male, Female]
-------------------------------------------------
child <- [Male, Female]
-------------------------------------------------
child_desc <- [Male, Female]
-------------------------------------------------
guard $ sexuality_pred parent1 parent2
-------------------------------------------------
guard $ childs_statement_is_valid child child_desc
-------------------------------------------------
guard $ parent1_statement_is_valid parent1 child_desc
-------------------------------------------------
guard $ parent2_statement_is_valid parent2 child child_desc
-------------------------------------------------
return $ PuzzleAnswer {
-------------------------------------------------
parent1=parent1,
-------------------------------------------------
parent2=parent2,
-------------------------------------------------
child=child,
-------------------------------------------------
child_desc=child_desc
-------------------------------------------------
}
-------------------------------------------------

------------------------------------------------- -- Run the program. We use mapM because we're applying a print in Monadic
-------------------------------------------------
-- setting. We use any kind of map because it's possible (due to poor coding)
-------------------------------------------------
-- that the solution could have more than one answer.
-------------------------------------------------
main = do
-------------------------------------------------
putStrLn "----------- Hetrosexual Couple -----------"
-------------------------------------------------
mapM_ print (solve_puzzle (/=))
-------------------------------------------------
putStrLn "----------- Gay Couple -----------"
-------------------------------------------------
mapM_ print (solve_puzzle (\x y -> x == y && x == Male))
-------------------------------------------------
putStrLn "----------- Lesbian Couple -----------"
-------------------------------------------------
mapM_ print (solve_puzzle (\x y -> x == y && x == Female))

You May Also Find These Documents Helpful

  • Better Essays

    Nuclear Weapons

    • 1325 Words
    • 6 Pages

    Nuclear Weapons On August 1954, the United States dropped two atomic bombs on the cites of Hiroshima and Nagasaki killing over two hundred thousands instantly and more over time due to radiation and other injuries. These two bombs ended World War II and changed the public’s view of nuclear energy. Those bombs were the only nuclear weapons used in any war, but there were many more detonated for tests.(1) During a speech about the nuclear test ban Kennedy said “Eighteen years ago the advent of nuclear…

    • 1325 Words
    • 6 Pages
    Better Essays
  • Better Essays

    Nuclear Weapons

    • 1352 Words
    • 6 Pages

    Nuclear Weapons Non-Proliferation is a limitation of production such as the Nuclear Non-Proliferation Treaty. The big five: United States, Britain, France, China, and former Soviet Union agreed to dismantle arsenals and signatory nations would not possess nuclear weapons (Conn). Although North Korea has become the world’s ninth nuclear power, they withdrew from the treaty in January 2003. Since then, North Korea processed enough plutonium for five nuclear bombs (Norris). America should and does…

    • 1352 Words
    • 6 Pages
    Better Essays
  • Good Essays

    nuclear weapon

    • 742 Words
    • 2 Pages

    after the second World War some countries have revised their nuclear weapons programme, nuclear weapons still today are considered to be an established currency of politics and security. While the nuclear weapons states have agreed on a legally binding commitment to nuclear disarmament, they continue to rely heavily on such weapons in their security doctrines and continue to invest and modernise their arsenals. The way towards nuclear disarmament has many different paths and there are several approaches…

    • 742 Words
    • 2 Pages
    Good Essays
  • Powerful Essays

    Nuclear Weapon

    • 1996 Words
    • 8 Pages

    that nuclear weapons are vital shields for the planet. They claim that nuclear weapons are deterrents that prevent the world from breaking out in total war. Researchers are supporting this argument by declaring how nuclear weapons have been keeping peace. However, other researchers and scientists deny the effectiveness of nuclear weapons as deterrents and declare that nuclear weapons will lead the world into total devastation. Many people throughout the world state that nuclear weapons have sparked…

    • 1996 Words
    • 8 Pages
    Powerful Essays
  • Good Essays

    nuclear weapon

    • 403 Words
    • 2 Pages

    11/27/13 Nuclear Weapons in Iran Just after the signing ceremony in Geneva on Sunday, President Hassan Rouhani of Iran declared that the world had recognized his country’s “nuclear rights.” He was right. The deal will only lead Iran to be more stubborn. Under the deal Iran is supposed…

    • 403 Words
    • 2 Pages
    Good Essays
  • Powerful Essays

    Nuclear Weapons

    • 2083 Words
    • 9 Pages

    Nuclear weapons were first developed by the United States prior to the start of World War ll. The first and only times that nuclear weapons were used in battle was against Japan in 1945. The United States dropped two different style of nuclear weapons on two different Japanese cities. One bomb used plutonium while the other used uranium to create the explosions. The bombs were dropped on Hiroshima on August 6, 1945 and Nagasaki on August 9, 1945. Estimates of deaths within the four months after…

    • 2083 Words
    • 9 Pages
    Powerful Essays
  • Good Essays

    Nuclear Weapon

    • 482 Words
    • 2 Pages

    Nuclear Weapons Since, a long period of time we have the invention of nuclear weapons of GLOBAL peace and constancy. Is it really right to say that nuclear weapons are peacemakers? Or they are killing weapons? Nuclear weapons are a volatile device that deduces its destructive force from the reactions of nuclear, either nuclear fission or a combination of both fission and fusion. Both chemical reaction releases huge quantities of energy from comparatively small amount matter. Simply, nuclear weapons…

    • 482 Words
    • 2 Pages
    Good Essays
  • Better Essays

    Nuclear Weapons

    • 1326 Words
    • 4 Pages

    Nuclear Weapons Definition: A nuclear weapon is an explosive device that derives its destructive force from nuclear reactions, either fission or a combination of fission and fusion. Types: There are two basic types of nuclear weapons: those that derive the majority of their energy from nuclear fission reactions alone, and those that use fission reactions to begin nuclear fusion reactions that produce a large amount of the total energy output. History: Starting with scientific breakthroughs made…

    • 1326 Words
    • 4 Pages
    Better Essays
  • Good Essays

    nuclear weapons

    • 704 Words
    • 3 Pages

    RELIGION ESSAY Nuclear weapons are the only type of weapon in existence that have the capacity to annihilate the human species and countless other species. Weapons are a problem with the catholic tradition as they go against the catholic virtue of peace and should be banned. The catholic virtue of peace is completely thrown away when it comes to nuclear weapons as they show anger and violent towards people and that’s not what it is about. If nuclear weapons were to be banned it would prevent death…

    • 704 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    Nuclear Weapons Outline

    • 330 Words
    • 2 Pages

    ID: 100307116 Department: Architecture Topic: Nuclear Weapons Research Paper Outline I.Introduction: Do we need nuclear weapons? Even though nuclear weapons give us advantages there are much more dangerous things than advantages such as a lot of people die when used, contries argue with each other because of these and there is evidence of radiation explosure. II.Topic Sentence: A lot of people die. A.We shouldn’t use these weapons even at war because not only soldiers but also civilians…

    • 330 Words
    • 2 Pages
    Satisfactory Essays