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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:52:41+00:00 2026-06-08T03:52:41+00:00

I’m trying to write a simple Fibonacci web server using WAI but I just

  • 0

I’m trying to write a simple Fibonacci web server using WAI but I just can’t figure out the types. This code is the essence of what I want to do, but it’s broken. The getQueryArg function returns a Maybe ByteString and I want to use that in my fibHandler function.

  1. How do I correctly handle the Maybe in my fibHandler?
  2. How do I call putStrLn on a Maybe? I’m trying fmap but I can’t seem to get it right.

 

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)
import Data.ByteString.Lazy.Char8 ()  -- Just for an orphan instance
import Control.Monad.IO.Class (liftIO)
import Data.Conduit
import Data.String.Utils
import Data.ByteString as BS (ByteString, putStrLn)
import Data.ByteString.Char8 as B (unpack)
import Data.Text as T (intercalate, pack, unpack)

app :: Application
app req
    | rawPathInfo req == "/fib" = fibHandler req
    | otherwise = notFoundHandler

fibHandler :: Request -> ResourceT IO Response
fibHandler req = do
    let nStr = getQueryArg (queryString req) "n"
    fmap (liftIO . BS.putStrLn) n
    let n = read nStr
    return $ responseLBS
        status200
        [("Content-Type", "text/plain")]
        (show $ fib n)

fib :: Int -> Int
fib n = foldl (*) 1 [1..n]

getQueryArg :: Query -> BS.ByteString -> Maybe BS.ByteString
getQueryArg [] key = Nothing
getQueryArg ((k,v):qs) key
    | k == key = Just v
    | otherwise = getQueryArg qs key

notFoundHandler :: ResourceT IO Response
notFoundHandler = return $ responseLBS
    status404
    [("Content-Type", "text/plain")]
    "Not found"

main :: IO ()
main = do
    BS.putStrLn $ "http://localhost:8080/"
    run 8080 $ app

[Update: a working copy of this code is here: https://gist.github.com/3145317]

  • 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-08T03:52:42+00:00Added an answer on June 8, 2026 at 3:52 am

    The simplest thing to do with Maybe values is to use case.

    case getQueryArg foo bar of
        Nothing -> {- something went wrong, write some code to report an error -}
        Just x  -> {- everything went okay, and x is the result of the successful computation -}
    

    Once you’ve done this a couple dozen times, you can graduate to the shorthand versions:

    maybe ({- went wrong -}) (\x -> {- successful x -}) (getQueryArg foo bar)
    fromMaybe {- default value -} (getQueryArg foo bar)
    traverse_ B.putStrLn (getQueryArg foo bar) -- this trick is a personal favorite
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.