aboutsummaryrefslogtreecommitdiff
path: root/ch03/3_b_9.hs
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2023-03-12 11:50:43 +0100
committerJan Sucan <jan@jansucan.com>2023-03-12 11:50:43 +0100
commitd5ff4ad02121f44332cc55d9be4040776ce3ac89 (patch)
tree78fb1f5ec13ad889cd771cd2fdf1f547204bbdf0 /ch03/3_b_9.hs
parent8e353802f75521d523fa526974792e569fec6758 (diff)
3_b_11: Add solution
Diffstat (limited to 'ch03/3_b_9.hs')
-rw-r--r--ch03/3_b_9.hs15
1 files changed, 15 insertions, 0 deletions
diff --git a/ch03/3_b_9.hs b/ch03/3_b_9.hs
index d276c7b..fd13801 100644
--- a/ch03/3_b_9.hs
+++ b/ch03/3_b_9.hs
@@ -5,6 +5,11 @@
--
-- 2. Write a function that calculates the turn made by three 2D points and
-- returns a Direction.
+--
+-- 3. Define a function that takes a list of 2D points and computes the
+-- direction of each successive triple. Given a list of points [a,b,c,d,e],
+-- it should begin by computing the turn made by [a,b,c], then the turn made
+-- by [b,c,d], then [c,d,e]. Your function should return a list of Direction.
data Direction = DLeft
| DRight
@@ -30,3 +35,13 @@ direction (x1, y1) (x2, y2) (x3, y3)
-- DLeft
-- ghci> direction (1,1) (2,2) (3,2)
-- DRight
+
+listDirections :: [Point] -> [Direction]
+listDirections (a:b:c:xs) = (direction a b c):(listDirections ([b,c] ++ xs))
+listDirections _ = []
+
+-- ghci> :l 3_b_9.hs
+-- [1 of 1] Compiling Main ( 3_b_9.hs, interpreted )
+-- Ok, one module loaded.
+-- ghci> listDirections [(1,1), (2,2), (3,3), (4,5), (6,1)]
+-- [DStraight,DLeft,DRight]