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 --- README.md | 2 +- ch03/3_b_9.hs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 993bce8..924d991 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ more visible in the list the first exercise of a group is in bold italics. | 3_b_8 | yes | | | | 3_b_9 | yes | | | | 3_b_10 | yes, in 3_b_9 | | | -| 3_b_11 | | | | +| 3_b_11 | yes, in 3_b_9 | | | | 3_b_12 | | | | | **_4_a_1_** | | 84 | 4. Functional programming | | 4_a_2 | | | | 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