-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path02-FPTurtle.fsx
108 lines (85 loc) · 2.77 KB
/
02-FPTurtle.fsx
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(* ======================================
02-FPTurtle.fsx
Part of "Thirteen ways of looking at a turtle"
Related blog post: http://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle/
======================================
Way #2: Simple FP - a module of functions with immutable state
In this design, the turtle state is immutable. A module contains functions that return a new turtle state,
and the client uses these turtle functions directly.
The client must keep track of the current state and pass it into the next function call.
====================================== *)
#load "Common.fsx"
open Common
// ======================================
// FP Turtle
// ======================================
// see code in this file
#load "FPTurtleLib.fsx"
open FPTurtleLib
// ======================================
// FP Turtle Client
// ======================================
module FPTurtleClient =
/// Function to log a message
let log message =
printfn "%s" message
// versions with log baked in (via partial application)
let move = Turtle.move log
let turn = Turtle.turn log
let penDown = Turtle.penDown log
let penUp = Turtle.penUp log
let setColor = Turtle.setColor log
let drawTriangle() =
Turtle.initialTurtleState
|> move 100.0
|> turn 120.0<Degrees>
|> move 100.0
|> turn 120.0<Degrees>
|> move 100.0
|> turn 120.0<Degrees>
// back home at (0,0) with angle 0
let drawThreeLines() =
Turtle.initialTurtleState
// draw black line
|> penDown
|> setColor Black
|> move 100.0
// move without drawing
|> penUp
|> turn 90.0<Degrees>
|> move 100.0
|> turn 90.0<Degrees>
// draw red line
|> penDown
|> setColor Red
|> move 100.0
// move without drawing
|> penUp
|> turn 90.0<Degrees>
|> move 100.0
|> turn 90.0<Degrees>
// back home at (0,0) with angle 0
// draw diagonal blue line
|> penDown
|> setColor Blue
|> turn 45.0<Degrees>
|> move 100.0
let drawPolygon n =
let angle = 180.0 - (360.0/float n)
let angleDegrees = angle * 1.0<Degrees>
// define a function that draws one side
let oneSide state sideNumber =
state
|> move 100.0
|> turn angleDegrees
// repeat for all sides
[1..n]
|> List.fold oneSide Turtle.initialTurtleState
// ======================================
// FP Turtle tests
// ======================================
(*
FPTurtleClient.drawTriangle() |> ignore
FPTurtleClient.drawThreeLines() |> ignore
FPTurtleClient.drawPolygon 4 |> ignore
*)