-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.4.lean
More file actions
59 lines (41 loc) · 1.49 KB
/
1.4.lean
File metadata and controls
59 lines (41 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-- Define a structure named RectangularPrism that contains the height, width, and depth
-- of a rectangular prism, each as a Float.
structure RectangularPrism where
height : Float
width : Float
depth : Float
deriving Repr
-- Define a function named volume : RectangularPrism → Float that computes the volume of
-- a rectangular prism.
def volume (rp:RectangularPrism) : Float := rp.depth * rp.height * rp.width
-- Define a structure named Segment that represents a line segment by its endpoints, and
-- define a function length : Segment → Float that computes the length of a line segment.
-- Segment should have at most two fields.
structure Segment where
point_a : Float
point_b : Float
deriving Repr
def length (s:Segment) : Float := ((s.point_a-s.point_b)^2)^(1/2)
#eval length (Segment.mk (-1) 4)
#eval length (Segment.mk (2.75) 8.25)
-- Which names are introduced by the declaration of RectangularPrism?
-- Answer: RectangularPrism.{mk,height,width,depth}
-- Which names are introduced by the following declarations of Hamster and Book?
-- What are their types?
structure Hamster where
name : String
fluffy : Bool
-- Answer:
-- Hamster.mk : (name:String) → (fluffy:Bool) -> Hamster
-- Hamster.name : String
-- Hamster.fluffy : Bool
structure Book where
makeBook ::
title : String
author : String
price : Float
-- Answer:
-- Book.makeBook : (title:String) → (author:String) → (price:Float) → Book
-- Book.title : String
-- Book.author : String
-- Book.price : Float