Preview

Homework 3

Good Essays
Open Document
Open Document
459 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Homework 3
(* 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

  • Good Essays

    Homework Chapter 4

    • 1675 Words
    • 7 Pages

    3. Allen visits Reno, Nevada, once a year to gamble. This year his gambling loss was $25,000. He commented to you, “At least I didn’t have to pay for my airfare and hotel room. The casino paid that because I am such a good customer. That was worth at least $3,000. “What are the relevant tax issues for Allen?…

    • 1675 Words
    • 7 Pages
    Good Essays
  • Satisfactory Essays

    Pt1420 Unit 6 Lab Report

    • 288 Words
    • 2 Pages

    Step 6 : Break the input string into square matrices of maximum possible size of…

    • 288 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    The free group with two generators a and b is composed of all finite strings that are formed from the four symbols a, a−1, b and b−1 given that no a is directly next to an a−1 and no b is directly next to a b−1. Two such strings can be concatenated and converted into a string of this type by repeatedly replacing the "forbidden" substrings with the empty string. For example: abab−1a−1 concatenated with abab−1a yields abab−1a−1abab−1a, which contains the substring a−1a underlined in the example, and so the “forbidden” string gets cancelled and reduced to abab−1bab−1a, which again contains the substring b−1b underlined in the example, which then gets reduced to abaab−1a. It can be checked that the group formed from the set of those strings with…

    • 493 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    a. The time in hours, minutes, and seconds is to be passed to a function named totsec().…

    • 720 Words
    • 3 Pages
    Satisfactory Essays
  • Satisfactory Essays

    O. 1 x1 Y. ---------- 7 S. x1 ---------- 0 x 1 = © Monica Dunbar & I Heart Grade 3 11 x1 ---------- 2. _____ (2) _____ _____ (12) (16) _____ _____ _____ _____ _____ _____ (14) (8) (8) (18) (10) (4) _____ _____…

    • 1388 Words
    • 41 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Homework Week 2

    • 743 Words
    • 3 Pages

    5. Bob enters a pizza shop and notices there are three different sizes of pizzas available. Sizes are given as the diameter of the pizza in inches. The cost of a pizza is based on the size. Bob would like to know which size of pizza has the lowest cost per square inch.…

    • 743 Words
    • 3 Pages
    Satisfactory Essays
  • Better Essays

    #include #include float avg_wt,avg_tt; int i = 0,ttl_wt=0,ttl_tt=0,qt; struct process { int prn; char type; int bt; int wt; int tt; struct process *nxt; }*stfor,*stbck,*np,*endfor,*endbck,*temp; void ins_node(struct process *np) { if(np->type == 'f') { if(stfor == NULL) stfor = endfor = np; else { endfor->nxt = np;…

    • 401 Words
    • 2 Pages
    Better Essays
  • Satisfactory Essays

    Homework 3

    • 830 Words
    • 4 Pages

    Molecule x blocked the ion channel receptor site so acetylcholine could not attach therefor not allowing the muscle to ultimately contract.…

    • 830 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    W8 Comp

    • 898 Words
    • 4 Pages

    2) If a variable holds the value "Ferrari," what is its data type? (Points : 5) Numeric Integer Floating point String…

    • 898 Words
    • 4 Pages
    Good Essays
  • Good Essays

    Homework 1 Assignment 1

    • 406 Words
    • 3 Pages

    7 A microwave transmitter typically requires a +8dBm level to drive an input fully. What is the actual voltage measured if +10dBm? Assume 600 ohm system. (2.45 V) = 10…

    • 406 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Assignment 3

    • 694 Words
    • 3 Pages

    e. Would you be likely to choose this as your bank or credit union? Why or why not? List at least two reasons for or against choosing this bank or credit union. (3-6 sentences. 2.0 points)…

    • 694 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    Assignment 3

    • 255 Words
    • 2 Pages

    6. Show that immigration, like population growth, tends to create unemployment in the short run and reduce the standard of living of workers in the long run, even though it does enhance the economy’s ability to produce.…

    • 255 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Assignment 3

    • 898 Words
    • 3 Pages

    c. List three facts about checking account options at the bank or credit union you chose. (3 sentences. 1.0 points)…

    • 898 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    The War of 1812 was one of the worst-fought wars in United States history because – of widespread disunity…

    • 4727 Words
    • 19 Pages
    Good Essays
  • Powerful Essays

    Assignment 3

    • 3296 Words
    • 17 Pages

    Working in groups, number in each group, determined by your teacher, you are required to deliver a 10 minute presentation to the class which informs them about the Orientation/Induction Program to be used for the new Store Manager at Triangle Tribe in Prahran. NOTE: Your program will be developed in accordance with the organisational procedures,observing confidentiality and privacy requirements.…

    • 3296 Words
    • 17 Pages
    Powerful Essays

Related Topics