[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Project Euler - A Case Study In "Specification Driven Programming"

Chris Kohlhepp

7/21/2015 9:44:00 PM

https://chriskohlhepp.wordpress.com/specification-driven-programming-in-co...

Algorithmic puzzles have become popular among computer programmers. While educational, solutions to these puzzles tend to promote approaches where competing aspects of the puzzle are entangled. Specification Driven or Constraint Based Programming is offered as an alternate approach. We hypothesize that this approach untangles the various aspect of the problem, making it easier to change the specifications of the problem while unifying test and implementation models.

We then present four project Euler problems and one NP-hard logic puzzle in support of our hypothesis. We hope the examples show the brevity and declarative power of functional programming combined with meta programming -presented here using Common Lisp.

In the epilogue we demonstrate how to selectively effect static typing using Lisp macros as well as implement constraints otherwise known as "Concepts" in C++.
3 Answers

mccoyrufus

1/19/2014 12:34:00 AM

0

On Sunday, January 12, 2014 4:25:01 PM UTC+11, harr...@gmail.com wrote:
> On Friday, January 10, 2014 10:27:52 PM UTC+8, piku...@gmail.com wrote:
>
> > Singapore civil servants can't even handle their job properly, much less the public. Case in point: The strike by Chinese bus drivers who had no choice when their complaints were not taken seriously or adequately addressed.
>
>
>
> That's because these people have been protected for far too long by the pap government. The reason the pap government protect the civil servants is to maintain their loyalty to the pap. Any civil servant, no matter how dumb and make the most serious mistake like losing $600 million in 6 months, will be forgiven and rewarded so long as he is loyalty to the pap. Conversely any civil servant, no matter how excellent and popular with the people, will be sacked, the moment his loyalty is suspect. How long can we tolerate this massive abuse by the pap, to entrench their rule.

That is why the great thinkers in America, put in their constitution, restricting a President to 2 terms of office. In that sense, even (so called "communist") China has a better system of government than singapore, 'cos their "executive branch", the President change every 10 (?) yrs.

William James

7/22/2015 9:07:00 AM

0

Chris Kohlhepp wrote:

> https://chriskohlhepp.wordpress.com/specification-driven-programming-in-co...


> Let's start with Project Euler Problem 1 : "If we list all the natural
> numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
> The sum of these multiples is 23. Find the sum of all the multiples of
> 3 or 5 below 1000."

Gauche Scheme:

(define (sum-multiples-below n)
(fold + 0
(filter
(^i (any (^j (zero? (mod i j))) '(3 5)))
(lrange 1 n))))

Another way:

(use srfi-42 :only (sum-ec))
(define (sum-multiples-below n)
(sum-ec (: i n)
(if (any (^j (zero? (mod i j))) '(3 5)))
i))

gosh> (sum-multiples-below 10)
23
gosh> (sum-multiples-below 1000)
233168


> "Each new term in the Fibonacci sequence is generated by adding
> the previous two terms. By starting with 1 and 2, the first 10
> terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By
> considering the terms in the Fibonacci sequence whose values do
> not exceed four million, find the sum of the even-valued terms."


(let go ((a 1) (b 2) (sum 0))
(if (<= a 4000000)
(go b (+ a b) (+ sum (if (even? a) a 0)))
sum))

===>
4613732



> "The prime factors of 13195 are 5, 7, 13 and 29. What is
> the largest prime factor of the number 600851475143?"


(use math.prime)
(last (naive-factorize 600851475143))

===>
6857



> "A palindromic number reads the same both ways. The largest
> palindrome made from the product of two 2-digit numbers is 9009
> = 91 x 99. Find the largest palindrome made from the product of
> two 3-digit numbers."


(define (reverse-integer n)
(let go ((n n) (r 0))
(if (zero? n)
r
(go (div n 10) (+ (* 10 r) (mod n 10))))))

(use srfi-42 :only (max-ec))

(max-ec (: i 1 1000) (: j 1 1000)
(:let prod (* i j))
(if (= prod (reverse-integer prod)))
prod)

===>
906609


--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... They've already said that everybody from
Syria can come to Sweden.... [T]hey are actually thinking of commandeering
people's vacation homes because they need more housing for immigrants.
--- Dr. Kevin MacDonald (http://lnrlive.com/tpc/tpc201...)

William James

2/9/2016 2:56:00 PM

0

WJ wrote:

> Chris Kohlhepp wrote:
>
> > https://chriskohlhepp.wordpress.com/specification-driven-programming-in-co...
>
>
> > Let's start with Project Euler Problem 1 : "If we list all the natural
> > numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
> > The sum of these multiples is 23. Find the sum of all the multiples of
> > 3 or 5 below 1000."
>
> Gauche Scheme:
>
> (define (sum-multiples-below n)
> (fold + 0
> (filter
> (^i (any (^j (zero? (mod i j))) '(3 5)))
> (lrange 1 n))))
>
> Another way:
>
> (use srfi-42 :only (sum-ec))
> (define (sum-multiples-below n)
> (sum-ec (: i n)
> (if (any (^j (zero? (mod i j))) '(3 5)))
> i))
>
> gosh> (sum-multiples-below 10)
> 23
> gosh> (sum-multiples-below 1000)
> 233168


MatzLisp (Ruby):

def sum_multiples_below n
(1...n).reduce(0){|sum,i|
sum + ([3,5].any?{|x| i.modulo(x).zero?} ? i : 0)}
end

sum_multiples_below 1000
==>233168


>
> > "The prime factors of 13195 are 5, 7, 13 and 29. What is
> > the largest prime factor of the number 600851475143?"
>
>
> (use math.prime)
> (last (naive-factorize 600851475143))
>
> ===>
> 6857


MatzLisp (Ruby):

require 'prime'

Prime.prime_division(600851475143).last.first
===>
6857

--
[T]he entire program ... involved deception from beginning to end. This is
suggested by the authors' clear political agenda and the pervasive double
standard in which gentile ethnocentrism and gentile adherence to cohesive
groups are seen as symptoms of psychopathology whereas ... no mention is made
of Jewish ethnocentrism or allegiance to cohesive groups.
--- Dr. Kevin MacDonald; "The Frankfurt School of Social Research and the
Pathologization of Gentile Group Allegiances"