From d5ff4ad02121f44332cc55d9be4040776ce3ac89 Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sun, 12 Mar 2023 11:50:43 +0100 Subject: 3_b_11: Add solution --- ch03/3_b_9.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'ch03') 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] -- cgit v1.2.3