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 8852683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:27:45+00:00 2026-06-14T13:27:45+00:00

I am trying to create a Haskell program which draws some simple 2d shapes

  • 0

I am trying to create a Haskell program which draws some simple 2d shapes to screen, but when you hover over each shape, it prints the line of source code where the shape was created.

In order to do this I would like to be able to create shapes with parameters for their dimensions and a final parameter which indicates the line number. Something like this:

rect1 = Shape(Rectangle 2 2 lineNumber)

This would create a rectangle of width 2 pixels, height 2 pixels, and use a function lineNumber to store the line this piece of code was written on. Does such a function exist in Haskell? Is it simple to create one?

I have searched stack overflow and found this question where the answerer suggests that the __LINE__ pragma from C++ can be used to achieve a similar effect. Is this the best way to go about it or is there a way to do it in pure Haskell?

  • 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-06-14T13:27:46+00:00Added an answer on June 14, 2026 at 1:27 pm

    You can do this with Template Haskell, which is technically yet another GHC extension, but is probably somehow more “pure” than C preprocessor.

    Code stolen from here and modified slightly.

    {-# LANGUAGE TemplateHaskell #-}
    
    module WithLocation (withLocation) where
    import Language.Haskell.TH
    
    withLocation' :: String -> IO a -> IO a
    withLocation' s f = do { putStrLn s ; f }
    
    withLocation :: Q Exp
    withLocation = withFileLine [| withLocation' |]
    
    withFileLine :: Q Exp -> Q Exp
    withFileLine f = do
        let loc = fileLine =<< location
        appE f loc
    
    fileLine :: Loc -> Q Exp
    fileLine loc = do
        let floc = formatLoc loc
        [| $(litE $ stringL floc) |]
    
    formatLoc :: Loc -> String
    formatLoc loc = let file = loc_filename loc
                        (line, col) = loc_start loc
                    in concat [file, ":", show line, ":", show col]
    

    Use it like this (from another module):

    {-# LANGUAGE TemplateHaskell #-}
    
    module Main where
    import WithLocation
    
    main = do
      $withLocation $ putStrLn "===oo0=Ü=0oo=== Kilroy was here"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying create the image enlargement effect when you hover your mouse over
I'm trying to create the ring Z/n (like normal arithmetic, but modulo some integer).
I’m trying to learn Haskell and I was trying to create a function that
I've recently been trying to learn me a Haskell, and I'd like to create
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a black line in my view to separate text blocks but
Trying to create a new Dedicated Cache Role in Windows Azure but get the
Trying to create a macro which can be used for print debug messages when
I am trying to teach my self F# by porting some Haskell Code. Specifily
I was trying to create a mail reader application using haskell. I found HaskellNet

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.