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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:28:55+00:00 2026-05-29T11:28:55+00:00

Since all the examples in the guide are with lists, I find it difficult

  • 0

Since all the examples in the guide are with lists, I find it difficult to see how to use pattern matching in Racket to write conditional matching like OCaml does, for example:

read ~var_a var_b s = match s.[0] with
    | _ when var_b >= var_a + 4 ->
        (* Do something *)
    | "a" when is_negative var_b -> 
        (* Do something else *)
    ...

How would I write something similar in Racket?

Thanks.

  • 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-29T11:28:56+00:00Added an answer on May 29, 2026 at 11:28 am

    The racket/match library includes pattern matching that can use arbitrary predicates through the ? pattern. Along with and, you should be able to get Racket’s matcher to behave. Although I’m a little weak in my OCaml, I think the following translation of the code above matches its meaning:

    (define (my-read #:var-a var-a var-b s)
      (match (string-ref s 0)
        [(and _
              (? (lambda (_)
                   (>= var-b (+ var-a 4)))))
         "do something"]
        [(and '#\a
              (? (lambda (_)
                   (< var-b 0))))
         "do something else"]))
    
    ;; Exercising the first case:     
    (my-read #:var-a 50
             60 "blah")
    
    ;; Exercising the second case:
    (my-read #:var-a 50
             -40 "alphabet")
    

    The ? matcher has an implicit and embedded within it, so the code can be expressed slightly more succinctly as:

    (define (my-read #:var-a var-a var-b s) 
      (match (string-ref s 0)
        [(? (lambda (_)
              (>= var-b (+ var-a 4))))
         "do something"]
        [(? (lambda (_)
              (< var-b 0))
            #\a)
         "do something else"]))
    

    In both, the lambdas in there aren’t watching what got matched, so I just named them _ to denote a don’t-care. But you can imagine more sophisticated patterns where the predicates could care deeply about what exactly got matched.

    Eli suggests using a general cond here, since there isn’t any significant pattern matching in the code. I agree. The code would look like this:

    (define (my-read #:var-a var-a var-b s) 
      (cond
        [(>= var-b (+ var-a 4))
         "do something"]
        [(and (char=? (string-ref s 0) #\a)
              (< var-b 0))
         "do something else"]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would using the adapter pattern be the best design to use when all you're
I am just curious since all TDD examples I have seen is web programming
What is the need of Collection framework in Java since all the data operations(sorting/adding/deleting)
I'm curious if this is possible at all since a select * from html
I cannot run ['abc'].append( MyModel.objects.all() ) since it generates exception 'NoneType' object is not
So I have a connection pool setup. Which is great and all since I
Situation: Site with content protected by username/password (not all controlled since they can be
I can't search for a particular string, since they're all very similar, but I'd
Since I've gotten no answer at all to my question Is there an alternative
Since both a Table Scan and a Clustered Index Scan essentially scan all records

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.