Paste: some declarative code!

Author: povman
Mode: factor
Date: Thu, 16 Feb 2012 12:51:43
Plain Text |
: hendersonsBudget ( -- price ) 8000 ;
: worthBudget ( -- price ) 8000 ;
: budget ( -- price ) hendersonsBudget worthBudget + ;
: ceremonyCateringPerHead ( -- price ) 5 ;
: ceremonyAttendants ( -- num ) 175 ;
: ceremonyCatering ( -- price )
    ceremonyAttendants ceremonyCateringPerHead * ;
: receptionPerHead ( -- price ) 60 ;
: receptionAttendants ( -- price ) 50 ;
: reception ( -- price )
    receptionPerHead receptionAttendants * ;
: venueHire ( -- price ) 2800 ;
: clothes ( -- price ) 2500 1000 + ;
: photos ( -- price ) 1000 ;
: flowers ( -- price ) 500 ;
: bands ( -- price ) 1000 ;
: preacher ( -- price ) 500 ;
: decor ( -- price ) 1000 ;
: drinks ( -- price ) 3 1250 / ceremonyAttendants 200 * * ;
! 1.25l bottles to go into 200ml glasses
: cake ( -- price ) 500 ;
: seats ( -- price ) 500 ;
: invitations ( -- price )
    receptionAttendants 10 / 30 *
    ceremonyAttendants 0.1 0.2 + * + ;
: total ( -- price )
    [ ceremonyCatering , clothes , photos ,
      flowers , preacher , decor , drinks , cake ,
      invitations , reception , seats ,
    ] { } make sum ;

Annotation: haskell version

Author: povman
Mode: haskell
Date: Thu, 16 Feb 2012 12:52:38
Plain Text |
> hendersonsBudget = 8000
> worthBudget = 8000
> budget = hendersonsBudget + worthBudget :: Double

> ceremonyCateringPerHead = 5
> ceremonyAttendants = 175
> ceremonyCatering = ceremonyAttendants * ceremonyCateringPerHead
> receptionPerHead = 60 -- 33.5
> receptionAttendants = 50
> reception = receptionPerHead * receptionAttendants
> venueHire = 2800
> clothes = 2500+1000
> photos = 1000
> flowers = 500
> bands = 1000
> preacher = 500
> decor = 1000
> drinks = (3 / 1.25e3) * (ceremonyAttendants * 200) -- 1.25l bottles to go into 200ml glasses
> cake = 500
> seats = 500
> invitations = (receptionAttendants / 10) * 30 + (ceremonyAttendants) * (0.1 + 0.2)

> total = sum [ceremonyCatering, clothes, photos, flowers, preacher, decor, drinks, cake, invitations, reception, seats] :: Double

Annotation: Slightly 'better' with macros

Author: povman
Mode: factor
Date: Thu, 16 Feb 2012 13:11:58
Plain Text |
MACRO: hendersonsBudget ( -- price ) [ 8000 ] ;
MACRO: worthBudget ( -- price ) [ 8000 ] ;
MACRO: budget ( -- price ) [ hendersonsBudget worthBudget + ] ;
MACRO: ceremonyCateringPerHead ( -- price ) [ 5 ] ;
MACRO: ceremonyAttendants ( -- num ) [ 175 ] ;
MACRO: ceremonyCatering ( -- price )
    [ ceremonyAttendants ceremonyCateringPerHead * ] ;
MACRO: receptionPerHead ( -- price ) [ 60 ] ;
MACRO: receptionAttendants ( -- price ) [ 50 ] ;
MACRO: reception ( -- price )
    [ receptionPerHead receptionAttendants * ] ;
MACRO: venueHire ( -- price ) [ 2800 ] ;
MACRO: clothes ( -- price ) [ 2500 1000 + ] ;
MACRO: photos ( -- price ) [ 1000 ] ;
MACRO: flowers ( -- price ) [ 500 ] ;
MACRO: bands ( -- price ) [ 1000 ] ;
MACRO: preacher ( -- price ) [ 500 ] ;
MACRO: decor ( -- price ) [ 1000 ] ;
MACRO: drinks ( -- price ) [ 3 1250 / ceremonyAttendants 200 * *
] ;
! 1.25l bottles to go into 200ml glasses
MACRO: cake ( -- price ) [ 500 ] ;
MACRO: seats ( -- price ) [ 500 ] ;
MACRO: invitations ( -- price )
    [ receptionAttendants 10 / 30 *
    ceremonyAttendants 0.1 0.2 + * + ] ;
MACRO: total ( -- price )
    [ { ceremonyCatering clothes photos 
      flowers preacher decor drinks cake
      invitations reception seats
     } sum ] ;

Annotation: Better still...

Author: povman
Mode: factor
Date: Thu, 16 Feb 2012 13:30:45
Plain Text |
SYNTAX: D:
    CREATE-WORD dup parse-definition [ ] curry (( -- x ))
    define-macro ;

D: hendersonsBudget 8000 ;
D: worthBudget 8000 ;
D: budget hendersonsBudget worthBudget + ;
D: ceremonyCateringPerHead 5 ;
D: ceremonyAttendants 175 ;
D: ceremonyCatering
    ceremonyAttendants ceremonyCateringPerHead * ;
D: receptionPerHead 60 ;
D: receptionAttendants 50 ;
D: reception
    receptionPerHead receptionAttendants * ;
D: venueHire 2800 ;
D: clothes 2500 1000 + ;
D: photos 1000 ;
D: flowers 500 ;
D: bands 1000 ;
D: preacher 500 ;
D: decor 1000 ;
D: drinks 3 1250 / ceremonyAttendants 200 * * ;
! 1.25l bottles to go into 200ml glasses
D: cake 500 ;
D: seats 500 ;
D: invitations
    receptionAttendants 10 / 30 *
    ceremonyAttendants 0.1 0.2 + * + ;
D: total
    [ ceremonyCatering , clothes , photos ,
      flowers , preacher , decor , drinks , cake ,
      invitations , reception , seats ,
     ] [ ] make sum ;

New Annotation

Summary:
Author:
Mode:
Body: