Homework 3

Good Essays
Topics: Variable, Object
(* Coursera Programming Languages, Homework 3, Provided Code *)

exception NoAnswer

datatype pattern = Wildcard | Variable of string | UnitP | ConstP of int | TupleP of pattern list | ConstructorP of string * pattern

datatype valu = Const of int | Unit | Tuple of valu list | Constructor of string * valu

fun g f1 f2 p = let val r = g f1 f2 in case p of Wildcard => f1 () | Variable x => f2 x | TupleP ps => List.foldl (fn (p,i) => (r p) + i) 0 ps | ConstructorP(_,p) => r p | _ => 0 end

(**** for the challenge problem only ****)

datatype typ = Anything | UnitT | IntT | TupleT of typ list | Datatype of string

(**** you can put all your code here ****)

(* 1 *) fun only_capitals (xs) = List.filter (fn(s)=>Char.isUpper(String.sub(s,0))) xs;

(* 2 *) fun longest_string1 (xs) = foldl (fn(x,y) => if String.size(x)>String.size(y) then x else y) "" xs;

(* 3 *) fun longest_string2 (xs) = foldl (fn(x,y) => if String.size(x)>=String.size(y) then x else y) "" xs;

(* 4 *) fun longest_string_helper (f:int*int->bool) (xs:string list) = foldl (fn(x,y) => if f(String.size(x),String.size(y)) then x else y) "" xs; val longest_string3 = longest_string_helper (fn(x,y) => x>y); val longest_string4 = longest_string_helper (fn(x,y) => x>=y);

(* 5 *) val longest_capitalized = longest_string1 o only_capitals;

(* 6 *) val rev_string = String.implode o List.rev o String.explode;

(* 7 *) fun first_answer f [] = raise NoAnswer | first_answer f (x::xs) = case f(x) of NONE => first_answer f xs | SOME v => v;
(* 8 *) fun all_answers f xs = let fun aux ([],acc) = SOME acc | aux (x::xs,acc) = case f(x) of NONE => NONE | SOME v =>

You May Also Find These Documents Helpful

  • Satisfactory Essays

    Homework 3

    • 671 Words
    • 3 Pages

    Finance Capstone Advanced Topics Homework Three AIJIA ZHOU The general information of the two bonds are shown below: Coupon | 10.625% | 4.25% | Coupon frequency | Semi-annual | Semi-annual | Coupon type | Fixed | Fixed | Day count | Act/Act | Act/Act | Issue date | August 15, 1985 | August 15, 2005 | Maturity date | August 15, 2015 | August 15, 2015 | Amount issued | 7.15 billion | 32.47 billion | Amount outstanding | 4.02 billion | 32.47 billion | Modified Duration Method…

    • 671 Words
    • 3 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Homework 3

    • 5294 Words
    • 119 Pages

    10/13/12 Assignment Print View Score: 45.94 1. out of 50 points (91.88%) aw ard: 10 out of 10.00 points Chapman Company obtains 100 percent of Abernethy Company’s stock on January 1, 2011. As of that date, Abernethy has the following trial balance: Debit Accounts payable Accounts receivable Additional paid-in capital Buildings (net) (4-year life) Cash and short-term investments Common stock Equipment (net) (5-year life) Inventory Land Long-term liabilities (mature…

    • 5294 Words
    • 119 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Homework 3

    • 830 Words
    • 4 Pages

    System: Neuromuscular Junction 1. What insulates each muscle cell? __________endomysium_______________ 2. Synaptic vesicles in the axon terminal of a motor neuron contain what neurotransmitter? __________acetylcholine_______________ 3. An action potential in the axon terminal of a motor neuron opens what type of ion channels? ____________Voltage-gated Calcium_____________ 4. By what means of membrane transport does the neurotransmitter leave the axon terminal? ____________exocytosis_____________…

    • 830 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    homework 3

    • 971 Words
    • 4 Pages

    University of Phoenix Material How Films Communicate Instructions Complete and submit Part I in your first week of class. In Week Two, complete Parts II & III after viewing a film of your choice from the University of Phoenix Material: How Films Communicate Film List located under your Week One materials. Submit your completed worksheet in Week Two. Part I: Beginning to Analyze Film To compete Part I, choose a movie you have viewed in the past (this does not have to be from the University…

    • 971 Words
    • 4 Pages
    Good Essays
  • Satisfactory Essays

    Homework #3 Solutions

    • 2621 Words
    • 11 Pages

    Physics 221 Summer 2012 HOMEWORK #3 Due Monday July 2, 2012 1 James Bond (90 kg), outfitted with perfectly matching skis and skiware, is at the top of a steep slope that a secret spy like him can easily handle. He lets himself go from rest and smoothly slides down the h = 15 m high hill. A big parking lot lies at the bottom of the hill. Since the parking area has been cleared of snow, the friction between the ground and the skis brings our hero to a halt at point D, located at a distance…

    • 2621 Words
    • 11 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Homework 3 PNG440W

    • 136 Words
    • 1 Page

    PNG 440W: Formation Evaluation Homework #3 Due in class, at 8:00 am on Thursday, October 23rd, 2014. Total points 100, grade weighting factor 3%. TASK: The litho-density log on the following page was logged in a borehole with freshwater mud and assuming limestone lithology (noted by “_LS” in legend at top of porosity track). For the layer from Zn_A to Zn_B, correct the average density porosity. The steps for doing this are: 1.) Estimate an average porosity value for this layer (should you use…

    • 136 Words
    • 1 Page
    Satisfactory Essays
  • Satisfactory Essays

    Homework Unit 3

    • 354 Words
    • 2 Pages

    PT1420 Unit 3 Homework Pg. 71 5. Module inputName () Declare string name Display"Enter Name:" End Module 6. In passing argument by value- any changes made to the parameter value won't affect argument outside module. In passing argument by reference- Changes will affect actual argument passed to module. Algorithm 3. Header- Module myModule (Integer a, integer b, integer c) Call statement - Call myModule (3,2,1) A contains 3, B contains 2, and C contains 1 4. Display "Here…

    • 354 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    unit 3 homework

    • 254 Words
    • 1 Page

    Unit 3 homework 1. What printing protocols are supported by CUPS? IPP, LPD/LPR, HTTP, SMB, JetDirect protocols 2. Of those protocols supported by CUPS, which one is the native CUPS protocol? IPP 3. On a Fedora system, there are three main tools or sets of tools for configuring a CUPS printer. What are they? system-config-printer, CUPS web interface, and lpadmin 4. What is a URI? (Give an explanation, not just an acronym expansion.) A Universal Resource Indicator is a standard for describing…

    • 254 Words
    • 1 Page
    Satisfactory Essays
  • Better Essays

    Macroeconomics Homework 3

    • 979 Words
    • 4 Pages

    11-24-12 Macroeconomics Homework 3 Questions 1. Intermediate goods are unfinished goods used in producing a final good or used up in the production process, which is a good produced for final consumption. Running shoes, watches, textbooks and sunscreen lotion are all final goods because they typically would not be used to make any other good they’re all finished goods for an end consumer. Cotton fibers, and lumber are intermediate goods which would be purchased for the production of a final good…

    • 979 Words
    • 4 Pages
    Better Essays
  • Satisfactory Essays

    Unit 3 Homework

    • 720 Words
    • 3 Pages

    Unit 3 Homework - Template Problem 1: Suppose that the supply schedule of Belgium Cocoa beans is as follows: Price of cocoa beans(per pound) | Quantity of cocoa beans supplied(pounds) | $40 | 700 | $35 | 600 | $30 | 500 | $25 | 400 | $20 | 300 | Suppose that Belgium cocoa beans can be sold only in Europe. The European demand schedule for Belgium cocoa beans is as follows: Price of Belgium cocoa beans(per pound) | Quantity of Belgium cocoa beans demanded(pounds) | $40…

    • 720 Words
    • 3 Pages
    Satisfactory Essays