SlideShare a Scribd company logo
1 of 6
Download to read offline
This	
  set	
  of	
  exercises	
  has	
  to	
  do	
  with	
  programming	
  with	
  recursively	
  constructed	
  lists.	
  In	
  
spirit,	
  the	
  exercises	
  build	
  upon	
  the	
  Nat	
  methods	
  you	
  wrote	
  for	
  the	
  previous	
  homework	
  
assignment.	
  
	
  
I	
  am	
  expecting	
  that	
  some	
  of	
  you	
  will	
  find	
  some	
  of	
  these	
  methods	
  tricky	
  to	
  implement	
  -­‐-­‐	
  
some	
  of	
  them	
  are	
  like	
  little	
  puzzles	
  -­‐-­‐	
  but	
  I	
  can't	
  anticipate	
  just	
  where	
  the	
  difficulties	
  will	
  
arise.	
  Please	
  post	
  to	
  piazza	
  as	
  you	
  encounter	
  hurdles.	
  
	
  
Note	
  that	
  each	
  method	
  has	
  a	
  simple	
  recursive	
  solution.	
  My	
  implementations	
  (admittedly	
  
making	
  frequent	
  use	
  of	
  the	
  inline	
  conditional	
  form	
  ? :	
  in	
  place	
  of	
  if/else	
  blocks)	
  are	
  all	
  one	
  
line	
  or	
  possibly	
  two	
  where	
  I've	
  decided	
  to	
  declare	
  a	
  local	
  variable.	
  Your	
  methods	
  don't	
  need	
  
to	
  be	
  that	
  concise	
  to	
  be	
  correct,	
  but	
  if	
  you	
  find	
  yourself	
  writing	
  many	
  lines	
  of	
  code	
  for	
  any	
  
method,	
  you	
  might	
  reconsider	
  your	
  approach.	
  
	
  
First,	
  here	
  is	
  an	
  implementation	
  of	
  a	
  PairOf	
  class	
  that	
  is	
  generalized	
  over	
  a	
  type	
  T
	
  
	
  
Next,	
  here	
  is	
  an	
  interface	
  for	
  predicates	
  on	
  items	
  of	
  some	
  variable	
  type	
  T.	
  Each	
  predicate	
  
implements	
  one	
  method,	
  a	
  true-­‐or-­‐false	
  test	
  on	
  an	
  item	
  of	
  type	
  T.	
  The	
  need	
  for	
  objects	
  of	
  
type	
  Pred	
  will	
  become	
  clear	
  later	
  in	
  this	
  writeup.	
  
 
	
  
Now,	
  a	
  generic	
  interface	
  for	
  "master's	
  program	
  linked	
  lists"	
  (which	
  we'll	
  abbreviate	
  MLL).	
  A	
  
few	
  of	
  these	
  methods	
  were	
  implemented	
  on	
  the	
  board	
  in	
  class,	
  but	
  most	
  weren't.	
  
	
  
For	
  the	
  methods	
  that	
  return	
  lists,	
  consult	
  the	
  implementation	
  of	
  append	
  given	
  in	
  lecture,	
  
which	
  gives	
  an	
  idea	
  how	
  to	
  go	
  about	
  deconstructing	
  the	
  problem	
  recursively.	
  
	
  
 
	
  
Some	
  example	
  predicate	
  classes	
  are	
  as	
  follows:	
  
	
  
 
	
  
To	
  complete	
  the	
  assignment,	
  complete	
  the	
  implementation	
  by	
  writing	
  Nil	
  and	
  Cons	
  (as	
  in	
  
class)	
  and	
  provide	
  a	
  tester	
  file.	
  The	
  first	
  lines	
  of	
  the	
  Nil	
  and	
  Cons	
  files	
  are	
  as	
  follows:	
  
	
  
	
  
	
  
Lest	
  you	
  get	
  the	
  wrong	
  impression,	
  type	
  variables	
  need	
  not	
  always	
  be	
  named	
  T.	
  Like	
  i	
  for	
  
integers	
  in	
  for	
  loops,	
  it's	
  just	
  the	
  most	
  common	
  variable	
  name,	
  by	
  far.	
  
	
  
 
	
  
 
	
  
	
  
	
  
	
  
	
  
	
  

More Related Content

Similar to 005

CS 111 - Homework 5 p. 1CS 111 - Homework 5Deadline1.docx
CS 111 - Homework 5 p. 1CS 111 - Homework 5Deadline1.docxCS 111 - Homework 5 p. 1CS 111 - Homework 5Deadline1.docx
CS 111 - Homework 5 p. 1CS 111 - Homework 5Deadline1.docx
annettsparrow
 
Linked List Problems
Linked List ProblemsLinked List Problems
Linked List Problems
Sriram Raj
 
Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review Guide
Benjamin Kissinger
 
Algorithm & data structure lec2
Algorithm & data structure lec2Algorithm & data structure lec2
Algorithm & data structure lec2
Abdul Khan
 

Similar to 005 (20)

JavaScript: Core Part
JavaScript: Core PartJavaScript: Core Part
JavaScript: Core Part
 
CS 111 - Homework 5 p. 1CS 111 - Homework 5Deadline1.docx
CS 111 - Homework 5 p. 1CS 111 - Homework 5Deadline1.docxCS 111 - Homework 5 p. 1CS 111 - Homework 5Deadline1.docx
CS 111 - Homework 5 p. 1CS 111 - Homework 5Deadline1.docx
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl
 
Basic matlab for beginners
Basic matlab for beginnersBasic matlab for beginners
Basic matlab for beginners
 
07 java variables
07   java variables07   java variables
07 java variables
 
Linked List Problems
Linked List ProblemsLinked List Problems
Linked List Problems
 
tutorial.ppt
tutorial.ppttutorial.ppt
tutorial.ppt
 
On Applying Or-Parallelism and Tabling to Logic Programs
On Applying Or-Parallelism and Tabling to Logic ProgramsOn Applying Or-Parallelism and Tabling to Logic Programs
On Applying Or-Parallelism and Tabling to Logic Programs
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and review
 
Presentation
PresentationPresentation
Presentation
 
PARADIGM IT.pptx
PARADIGM IT.pptxPARADIGM IT.pptx
PARADIGM IT.pptx
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
Conventional and Reasonable
Conventional and ReasonableConventional and Reasonable
Conventional and Reasonable
 
Data weave 2.0 advanced (recursion, pattern matching)
Data weave 2.0   advanced (recursion, pattern matching)Data weave 2.0   advanced (recursion, pattern matching)
Data weave 2.0 advanced (recursion, pattern matching)
 
ML_in_QM_JC_02-10-18
ML_in_QM_JC_02-10-18ML_in_QM_JC_02-10-18
ML_in_QM_JC_02-10-18
 
Basics java programing
Basics java programingBasics java programing
Basics java programing
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
 
Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review Guide
 
Algorithm & data structure lec2
Algorithm & data structure lec2Algorithm & data structure lec2
Algorithm & data structure lec2
 
Implementation
ImplementationImplementation
Implementation
 

005

  • 1. This  set  of  exercises  has  to  do  with  programming  with  recursively  constructed  lists.  In   spirit,  the  exercises  build  upon  the  Nat  methods  you  wrote  for  the  previous  homework   assignment.     I  am  expecting  that  some  of  you  will  find  some  of  these  methods  tricky  to  implement  -­‐-­‐   some  of  them  are  like  little  puzzles  -­‐-­‐  but  I  can't  anticipate  just  where  the  difficulties  will   arise.  Please  post  to  piazza  as  you  encounter  hurdles.     Note  that  each  method  has  a  simple  recursive  solution.  My  implementations  (admittedly   making  frequent  use  of  the  inline  conditional  form  ? :  in  place  of  if/else  blocks)  are  all  one   line  or  possibly  two  where  I've  decided  to  declare  a  local  variable.  Your  methods  don't  need   to  be  that  concise  to  be  correct,  but  if  you  find  yourself  writing  many  lines  of  code  for  any   method,  you  might  reconsider  your  approach.     First,  here  is  an  implementation  of  a  PairOf  class  that  is  generalized  over  a  type  T     Next,  here  is  an  interface  for  predicates  on  items  of  some  variable  type  T.  Each  predicate   implements  one  method,  a  true-­‐or-­‐false  test  on  an  item  of  type  T.  The  need  for  objects  of   type  Pred  will  become  clear  later  in  this  writeup.  
  • 2.     Now,  a  generic  interface  for  "master's  program  linked  lists"  (which  we'll  abbreviate  MLL).  A   few  of  these  methods  were  implemented  on  the  board  in  class,  but  most  weren't.     For  the  methods  that  return  lists,  consult  the  implementation  of  append  given  in  lecture,   which  gives  an  idea  how  to  go  about  deconstructing  the  problem  recursively.    
  • 3.     Some  example  predicate  classes  are  as  follows:    
  • 4.     To  complete  the  assignment,  complete  the  implementation  by  writing  Nil  and  Cons  (as  in   class)  and  provide  a  tester  file.  The  first  lines  of  the  Nil  and  Cons  files  are  as  follows:         Lest  you  get  the  wrong  impression,  type  variables  need  not  always  be  named  T.  Like  i  for   integers  in  for  loops,  it's  just  the  most  common  variable  name,  by  far.    
  • 6.