Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:11:17+00:00 2026-05-26T14:11:17+00:00

I almost finish the chapter 3 of Real World Haskell. The last exercise blocks

  • 0

I almost finish the chapter 3 of Real World Haskell. The last exercise blocks me. My code will crash while running. Does anyone can tell me which part is wrong in my code? Thanks.

Question:
Using the code from the preceding three exercises, implement Graham’s scan algorithm for the convex hull of a set of 2D points. You can find good description of what a convex hull is, and how the Graham scan algorithm should work, on Wikipedia.

Answer:

-- Graham Scan, get the convex hull.
-- Implement the algorithm on http://en.wikipedia.org/wiki/Graham_scan

import Data.List
import Data.Ord

data Direction = TurnLeft | TurnRight | GoStraight deriving (Eq, Show)

-- Determine if three points constitute a "left turn" or "right turn" or "go straight".
-- For three points (x1,y1), (x2,y2) and (x3,y3), simply compute the direction of the cross product of the two vectors defined by points (x1,y1), (x2,y2) and (x1,y1), (x3,y3), characterized by the sign of the expression (x2 − x1)(y3 − y1) − (y2 − y1)(x3 − x1). If the result is 0, the points are collinear; if it is positive, the three points constitute a "left turn", otherwise a "right turn".
direction a b c = case compare ((x2 - x1) * (y3 - y1)) ((y2 - y1) * (x3 - x1)) of
    EQ -> GoStraight
    GT -> TurnLeft
    LT -> TurnRight
    where   x1 = fst a
            y1 = snd a
            x2 = fst b
            y2 = snd b
            x3 = fst c
            y3 = snd c

grahamScan points = scan (sort points)
            -- For each point, it is determined whether moving from the two previously considered points to this point is a "left turn" or a "right turn". If it is a "right turn", this means that the second-to-last point is not part of the convex hull and should be removed from consideration. This process is continued for as long as the set of the last three points is a "right turn". As soon as a "left turn" is encountered, the algorithm moves on to the next point in the sorted array. (If at any stage the three points are collinear, one may opt either to discard or to report it, since in some applications it is required to find all points on the boundary of the convex hull.)
    where   scan (a : (b : (c : points)))
                | (direction a b c) == TurnRight    = scan (a : (c : points))
                | otherwise                         = a : (scan (b : (c : points)))
            scan (a : (b : []))                     = []
            -- Put prime to the head.
            sort points                             = prime : (reverse (sortBy (compareByCosine prime) rest))
                        -- Sort a list to compute. The first step is to find a point whose y-coordinate is lowest. If there are more than one points with lowest y-coordinate, take the one whose x-coordinate is lowest. I name it prime.
                where   compareByYAndX a b
                            | compareByY == EQ  = comparing fst a b
                            | otherwise         = compareByY
                            where   compareByY  = comparing snd a b 
                        prime                   = minimumBy compareByYAndX points
                        -- Delete prime from the candidate points. Sort the rest part by the cosine value of the angle between each vector from prime to each point. Reverse it and put prime to the head.
                        rest                    = delete prime points
                        compareByCosine p a b   = comparing cosine pa pb
                            where   pa          = (fst a - fst p, snd a - snd p)
                                    pb          = (fst b - fst p, snd b - snd p)
                                    cosine v    = x / sqrt (x ^ 2 + y ^ 2)
                                        where   x = fst v
                                                y = snd v
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:11:17+00:00Added an answer on May 26, 2026 at 2:11 pm

    When doing the scan on the sorted set of points, when you finish, you throw away points that are supposed to be in the hull. In this line

    scan (a : (b : [])) = []
    

    you are deleting the last two points in the hull. Really it should be

    scan (a : (b : [])) = [a, b]
    

    or even better,

    scan ps = ps
    

    which covers cases with only one point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Almost every major news media outlet is reporting that Apple will stop using Google
I have a very cluttered code, and the current revision is almost complete, meaning
I came up with a problem right now when my game is almost finish.
I almost finish my django web aplication, but i never using shell for it.
I need some suggestion. I am almost finish with my program but stuck at
I'm trying to finish a site for my friends brother, and everything (almost) works
I am trying to finish a smoosh() method which will takes an array of
Im almost finish with my game made with Adobe CS5.5 AIR 3.0 for iOS.
I have an app that I've almost finish now that emails, at the end
I am building a brush app, it's almost finish and what i did was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.