Difference between revisions of "Sum Distances To Origin With Functions Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
(Created page with "thumb =Background= [https://en.wikipedia.org/wiki/Pythagorean_theorem Pythagorean theorem] The distance of a point (x,y) from the origin: <math>dist...")
 
 
(11 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
<math>distance = \sqrt {x^2 + y^2}</math>
 
<math>distance = \sqrt {x^2 + y^2}</math>
  
==Basis Library==
+
=Code to Use=
===Math===
+
=={{ListStructure}}==
[https://smlfamily.github.io/Basis/math.html#SIG:MATH.sqrt:VAL Math.sqrt(x)]
+
{{ListNull}}
 +
{{ListHd}}
 +
{{ListTl}}
 +
 
 +
==[https://smlfamily.github.io/Basis/math.html Math]==
 +
* [https://smlfamily.github.io/Basis/math.html#SIG:MATH.sqrt:VAL Math.sqrt(x)]
 +
 
 +
==Tuple==
 +
* [https://courses.cs.washington.edu/courses/cse341/19sp/unit1notes.pdf#section*.6 Grossman Notes]
  
 
=Code To Implement=
 
=Code To Implement=
 
==sum_distances_to_origin==
 
==sum_distances_to_origin==
Fall 2022 Note: if attempted without pattern matching, you may receive an unresolved flex record error:
+
{{SMLToImplement|sum_distances_to_origin_fun|sum_distances_to_origin|sum_distances_to_origin_fun}}
 
 
../../../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}}
+
A list of tuples <code>xys</code> is passed to <code>sum_distances_to_origin</code>.  Evaluate to the sum of the distances from each point (x,y) in the list to the origin.
  
[https://smlfamily.github.io/Basis/math.html#SIG:MATH.sqrt:VAL Math.sqrt(v)]
+
<syntaxhighlight lang="sml">
 +
fun sum_distances_to_origin(xys : (real * real) list) : real =
 +
raise Fail "NotYetImplemented"
 +
</syntaxhighlight>
  
fun sum_distances_to_origin(xys : (real * real) list)
+
For example, if the list is <code>[(3,4), (5,12)]</code> then calling <code>sum_distances_to_origin</code> with that list would evaluate to <code>18</code> (since 5 + 13 is 18).
  
 
=Test=
 
=Test=
{{SMLUnitTesting|run_sum_distances_to_origin_fun_testing|warmup_sum_distances_to_origin_fun}}
+
{{SMLUnitTesting|run_sum_distances_to_origin_fun_testing|sum_distances_to_origin_fun}}

Latest revision as of 19:42, 30 August 2024

Pythagorean.svg

Background

Pythagorean theorem

The distance of a point (x,y) from the origin:

Code to Use

List

  • null is the passed in list empty?
  • hd head element of the passed in list
  • tl tail list of the passed in list

Math

Tuple

Code To Implement

sum_distances_to_origin

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

A list of tuples xys is passed to sum_distances_to_origin. Evaluate to the sum of the distances from each point (x,y) in the list to the origin.

fun sum_distances_to_origin(xys : (real * real) list) : real =
	raise Fail "NotYetImplemented"

For example, if the list is [(3,4), (5,12)] then calling sum_distances_to_origin with that list would evaluate to 18 (since 5 + 13 is 18).

Test

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

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

SML Error Messages