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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:13:46+00:00 2026-06-02T03:13:46+00:00

I am trying to work through the exercises in Write Yourself a Scheme in

  • 0

I am trying to work through the exercises in Write Yourself a Scheme in 48 Hours. I need help with simplifying couple of functions.

data LispVal = Number Integer
             | String String
             | Bool Bool

isNumber :: [LispVal] -> LispVal
isNumber []               = Bool False 
isNumber [(Number _)]     = Bool True
isNumber ((Number _):xs)  = isNumber xs
isNumber _                = Bool False

isString :: [LispVal] -> LispVal
isString []               = Bool False 
isString [(String _)]     = Bool True
isString ((String _):xs)  = isString xs
isString _                = Bool False

The isNumber and isString functions have lot common structure. How do I go about factoring out this common structure?

  • 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-02T03:13:47+00:00Added an answer on June 2, 2026 at 3:13 am

    While you can’t parameterize the pattern match itself, you can write yourself small helper functions so you at least don’t have to repeat the list handling for every function:

    isString (String _) = True
    isString _ = False
    
    isNumber (Number _) = True
    isNumber _ = False
    
    all1 _ [] = False
    all1 f xs = all f xs
    
    isListOfStrings = Bool . all1 isString
    isListOfNumbers = Bool . all1 isNumber
    

    In my opinion, the special case handling of the empty list isn’t consistent here. You should consider just using all instead (so that the empty list can be a list of any kind, similar to how Haskell’s lists work).

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

Sidebar

Related Questions

I'm trying to work my way through the exercises at the bottom of this
I,m trying to work through and test a Voice Recognition example based on the
Hey all, here is the issue I am currently trying to work through. We
I'm trying to work my way through an Objective-C tutorial. In the book there
I am trying to get time with microseconds through glib which will work on
I am trying to work through how to publish items from multiple arrays while
I'm trying to work through a problem where I'm mapping EF Entities to POCO
I am trying to work through this question and I have had little success
I am trying to work through the HelloWorld example on the Web that shows
I'm trying to work through a book on C and I am stuck on

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.