Difference between revisions of "Sum Distances To Origin With Pattern Matching Compared To Java Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
<!--
 
 
=Motivation=
 
=Motivation=
Compare implementations in Java and SML to highlight what pattern matching provides.
+
Compare implementations in Java and SML to highlight the crispness of type inference and pattern matching.
-->
 
  
[[File:Pythagorean.svg|thumb]]
+
=Previous Warmup=
 +
This warmup is a variation of the [[Sum_Distances_To_Origin_With_Functions_Assignment|Sum Distances To Origin with Functions]] warmup.
  
 
=Background=
 
=Background=
[https://en.wikipedia.org/wiki/Pythagorean_theorem Pythagorean theorem]
 
 
The distance of a point (x,y) from the origin:
 
 
<math>distance = \sqrt {x^2 + y^2}</math>
 
 
 
==SML==
 
==SML==
 
[https://smlfamily.github.io/Basis/math.html#SIG:MATH.sqrt:VAL Math.sqrt(x)]
 
[https://smlfamily.github.io/Basis/math.html#SIG:MATH.sqrt:VAL Math.sqrt(x)]
Line 31: Line 24:
  
 
==SML==
 
==SML==
 
+
{{SMLToImplement|sum_distances_to_origin|sum_distances_to_origin|warmup_sum_distances_to_origin_pattern}}
Fall 2022 Note: if attempted without pattern matching, you may receive an unresolved flex record error:
 
 
 
../../../main/sml/warmup_sum_distances_to_origin/sum_distances_to_origin.sml:4.2-21.56 Error: unresolved flex record (need to know the names of ALL the fields
 
in this context)
 
  type: {1:real, 2:real; 'Z}
 
 
 
this can be solved by using pattern matching or specifying the type of the parameter xys:
 
 
 
fun sum_distances_to_origin(xys : (real * real) list)
 
 
 
{{SMLToImplement|sum_distances_to_origin|sum_distances_to_origin|warmup_sum_distances_to_origin}}
 
  
 
[https://smlfamily.github.io/Basis/math.html#SIG:MATH.sqrt:VAL Math.sqrt(v)]
 
[https://smlfamily.github.io/Basis/math.html#SIG:MATH.sqrt:VAL Math.sqrt(v)]
Line 53: Line 35:
  
 
==SML==
 
==SML==
{{SMLUnitTesting|run_sum_distances_to_origin_testing|warmup_sum_distances_to_origin}}
+
{{SMLUnitTesting|run_sum_distances_to_origin_pattern_testing|warmup_sum_distances_to_origin_pattern}}

Latest revision as of 14:33, 12 September 2022

Motivation

Compare implementations in Java and SML to highlight the crispness of type inference and pattern matching.

Previous Warmup

This warmup is a variation of the Sum Distances To Origin with Functions warmup.

Background

SML

Math.sqrt(x)

Java

Math.sqrt(x)

Code To Implement

Java

Note: you will need to complete the ImList<E> exercise before testing the Java portion of this assignment.

class: SumDistancesToOrigin.java Java.png
methods: sumDistancesToOrigin
package: distance.warmup
source folder: src/main/java

Math.sqrt(v)

public static double sumDistancesToOrigin(ImList<ImTuple2<Double, Double>> xys)

SML

file: src/main/sml/warmup_sum_distances_to_origin_pattern/sum_distances_to_origin.sml Smlnj-logo.png
functions: sum_distances_to_origin

Math.sqrt(v)

fun sum_distances_to_origin(xys : (real * real) list)

Test

Java

class: SumDistancesToOriginTestSuite.java Junit.png
package: distance.warmup
source folder: src/test/java

SML

source folder: src/test/sml/warmup_sum_distances_to_origin_pattern
how to run with CM.make verbosity off: sml -Ccm.verbose=false run_sum_distances_to_origin_pattern_testing.sml
how to run with CM.make verbosity on: sml run_sum_distances_to_origin_pattern_testing.sml

note: ensure that you have removed all printing to receive credit for any assignment.

SML Error Messages