Nuclear Weapons

Only available on StudyMode
  • Download(s) : 62
  • Published : December 5, 2012
Open Document
Text Preview
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:...
tracking img