Skip to content

Commit 1ee88ed

Browse files
author
Oskar Lundström
committed
Löste todos i Dim.ValueLevel
1 parent 1af49c3 commit 1ee88ed

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

Physics/src/Dimensions/ValueLevel.lhs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ From the introduction, two things became apparanent:
99

1010
We'll use these facts when implementing dimensions. More precisely, "length" and "metre" will be interchangable, and so on.
1111

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.
1513

1614
> module Dimensions.ValueLevel
1715
> ( Dim(..)
@@ -56,6 +54,8 @@ Velocity is $m/s$ or equivalently $m^1*s^{ -1 }$. This explains why the exponent
5654

5755
Noticed how we used "m" (for metre) for implicitly refering to the dimension "length"? It's quite natural to work this way.
5856

57+
---
58+
5959
**Exercise.** Create values for acceleration, area and charge.
6060

6161
**Solution.**
@@ -64,6 +64,8 @@ Noticed how we used "m" (for metre) for implicitly refering to the dimension "le
6464
> area = Dim 2 0 0 0 0 0 0
6565
> charge = Dim 0 0 1 1 0 0 0
6666

67+
---
68+
6769
Multiplication and division
6870
---------------------------
6971

@@ -73,6 +75,8 @@ Dimensions can be multiplied and divided. Velocity is, as we just saw, a divisio
7375
> (Dim le1 ma1 ti1 cu1 te1 su1 lu1) `mul` (Dim le2 ma2 ti2 cu2 te2 su2 lu2) =
7476
> Dim (le1+le2) (ma1+ma2) (ti1+ti2) (cu1+cu2) (te1+te2) (su1+su2) (lu1+lu2)
7577

78+
---
79+
7680
**Exercise.** Implement a function for dividing two dimensions.
7781

7882
**Solution.**
@@ -81,6 +85,8 @@ Dimensions can be multiplied and divided. Velocity is, as we just saw, a divisio
8185
> (Dim le1 ma1 ti1 cu1 te1 su1 lu1) `div` (Dim le2 ma2 ti2 cu2 te2 su2 lu2) =
8286
> Dim (le1-le2) (ma1-ma2) (ti1-ti2) (cu1-cu2) (te1-te2) (su1-su2) (lu1-lu2)
8387

88+
---
89+
8490
It's now possible to construct dimensions in the following fashion.
8591

8692
> velocity' = length `div` time
@@ -90,32 +96,33 @@ It's now possible to construct dimensions in the following fashion.
9096

9197
TODO: try to replace "since" by "because" (in many places) to avoid confusion with the meaning of "since" in the time-domain.
9298

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
94100

95101
\begin{align}
96102
F_{friction} = \mu * F_{normal} \iff \mu = \frac{F_{friction}}{F_{normal}}
97103
\end{align}
98104

105+
---
106+
99107
**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.
100108

101109
**Solution.**
102110

103111
> one = Dim 0 0 0 0 0 0 0
104112
> one' = force `div` force
105113

114+
---
115+
106116
Pretty-printer
107117
--------------
108118

109119
![A not so pretty printer](Printer.png){.float-img-left}
110120

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
112122

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
115124

116-
> len :: (Integral n) => [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.
119126

120127
> showDim :: Dim -> String
121128
> 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
127134
> pos = filter (\(_, exp) -> exp > 0) paired
128135
> neg = filter (\(_, exp) -> exp < 0) paired
129136
> neg' = map (\(d, exp) -> (d, -exp)) neg
130-
137+
>
131138
> f (u,1) = u
132139
> f (u,n) = u ++ "^" ++ show n
133-
140+
>
134141
> posStrs = map f pos
135142
> negStrs = map f neg'
136-
> posStr = if null posStrs -- make a function for this repeated pattern (and use it twice: in posStr ad negStr)
137-
> then ""
138-
> else foldl1 (\strs str -> str ++ "*" ++ strs) posStrs
143+
> posStr = strsToStr posStrs
144+
> negStr = strsToStr negStrs
139145
> (left, right) = if len negStrs > 1
140146
> then ("(", ")")
141147
> else ("", "")
142-
> negStr = if null negStrs
143-
> then ""
144-
> else foldl1 (\strs str -> str ++ "*" ++ strs) negStrs
145148
> negStr' = left ++ negStr ++ right
146-
149+
> strsToStr :: [String] -> String
150+
> strsToStr strs = if null strs
151+
> then ""
152+
> else foldl1 (\strs' str -> str ++ "*" ++ strs') strs
153+
> len :: (Integral n) => [a] -> n
154+
> len [] = 0
155+
> len (a:as) = 1 + len as
156+
>
147157
> instance Show Dim where
148158
> show = showDim
149159

@@ -154,8 +164,4 @@ Now dimensions are printed quite pretty in GHCi.
154164

155165
Note that the SI-unit of the dimensions is used for printing.
156166

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

Comments
 (0)