You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Physics/src/Dimensions/ValueLevel.lhs
+30-24Lines changed: 30 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -9,9 +9,7 @@ From the introduction, two things became apparanent:
9
9
10
10
We'll use these facts when implementing dimensions. More precisely, "length" and "metre" will be interchangable, and so on.
11
11
12
-
TODO: Explain (or link to explanation of) Haskell module syntax, and why you include it.
13
-
14
-
TODO: I suggest you use the following layout pattern for the module headers (indent 4 spaces, put "where" after the closing paren).
12
+
What you see below is the [Haskell module syntax](http://learnyouahaskell.com/modules#making-our-own-modules). Why do we show it to you? Because we don't want to hide any code from you if you follow along this tutorial.
15
13
16
14
> moduleDimensions.ValueLevel
17
15
> ( Dim(..)
@@ -56,6 +54,8 @@ Velocity is $m/s$ or equivalently $m^1*s^{ -1 }$. This explains why the exponent
56
54
57
55
Noticed how we used "m" (for metre) for implicitly refering to the dimension "length"? It's quite natural to work this way.
58
56
57
+
---
58
+
59
59
**Exercise.** Create values for acceleration, area and charge.
60
60
61
61
**Solution.**
@@ -64,6 +64,8 @@ Noticed how we used "m" (for metre) for implicitly refering to the dimension "le
64
64
> area =Dim2000000
65
65
> charge =Dim0011000
66
66
67
+
---
68
+
67
69
Multiplication and division
68
70
---------------------------
69
71
@@ -73,6 +75,8 @@ Dimensions can be multiplied and divided. Velocity is, as we just saw, a divisio
It's now possible to construct dimensions in the following fashion.
85
91
86
92
> velocity' =length`div` time
@@ -90,32 +96,33 @@ It's now possible to construct dimensions in the following fashion.
90
96
91
97
TODO: try to replace "since" by "because" (in many places) to avoid confusion with the meaning of "since" in the time-domain.
92
98
93
-
A dimension we so far haven't mentioned is the *scalar*, which shows up when working with, for example, coefficients of friction. It's dimensionless since it arises from division of two equal dimensions. The case of coefficients of friction looks like
99
+
A dimension we so far haven't mentioned is the *scalar*, which shows up when working with, for example, coefficients of friction. It's dimensionless because it arises from division of two equal dimensions. The case of coefficients of friction looks like
**Exercise.** Create two values, which represent the scalar. They should of course have the same value, but be created in two different ways. One by writing the exponents explicitly. One by dividing two equal dimensions.
100
108
101
109
**Solution.**
102
110
103
111
> one =Dim0000000
104
112
> one' = force `div` force
105
113
114
+
---
115
+
106
116
Pretty-printer
107
117
--------------
108
118
109
119
{.float-img-left}
110
120
111
-
The purpose of value-level dimensions is to be able to print 'em nicely. So let's create a pretty-printer.
121
+
The purpose of value-level dimensions is to be able to print 'em nicely. The pretty printer should be function with the type
112
122
113
-
TODO: move "len" out of the way.
114
-
TODO: I think this code is better left out (not part of the learning outcomes, or something like that). Its type + some example results may be useful, though.
123
+
< showDim::Dim->String
115
124
116
-
> len:: (Integraln) => [a] ->n
117
-
> len []=0
118
-
> len (a:as) =1+ len as
125
+
meaning it shows a dimension as a string. How to actually implement this is not the interesting part in this tutorial, so you may with good conscience skip this implementation. However, if you're curios, you can still take a look at it.
119
126
120
127
> showDim::Dim->String
121
128
> showDim (Dim le ma ti cu te su lu)
@@ -127,23 +134,26 @@ TODO: I think this code is better left out (not part of the learning outcomes, o
127
134
> pos =filter (\(_, exp) ->exp>0) paired
128
135
> neg =filter (\(_, exp) ->exp<0) paired
129
136
> neg' =map (\(d, exp) -> (d, -exp)) neg
130
-
137
+
>
131
138
> f (u,1) = u
132
139
> f (u,n) = u ++"^"++show n
133
-
140
+
>
134
141
> posStrs =map f pos
135
142
> negStrs =map f neg'
136
-
> posStr =ifnull posStrs -- make a function for this repeated pattern (and use it twice: in posStr ad negStr)
@@ -154,8 +164,4 @@ Now dimensions are printed quite pretty in GHCi.
154
164
155
165
Note that the SI-unit of the dimensions is used for printing.
156
166
157
-
TODO: Rephrase: if Quantity.lhs really is the "natural" next step, then make it the next step. Otherwise, if it just _seems to be_ the natural next step, explain why your next step is better (more natural?).
158
-
159
-
The result from this section is the ability to multiply, divide and print dimensions. The next natural step would be to create a data type for quantities. However, we'll first implement type-level dimensions.
160
-
161
-
TODO: Actually, the real "next step" according to the link is now "Testing of ...". Update the text to match. [Testing seems pretty natural to me.]
167
+
The result from this section is the ability to multiply, divide and print dimensions. The next step is to test these operations and see if they actually work.
0 commit comments