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

  • Home
  • SEARCH
  • 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 6044341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:00:02+00:00 2026-05-23T07:00:02+00:00

As a follow up to my previous question , I am trying to implement

  • 0

As a follow up to my previous question, I am trying to implement a simple pattern matching in Clojure.

I would like something like the following:

(match target
  [ub]    expr1    ; ub should be bound to actual value in expr1
  ['< ub] expr2    ; match the literal less-than symbol
                   ; and ub should be bound to actual value in expr2
  [lb ub] expr3    ; lb and ub should be bound to actual values in expr3
  :else   expr4    ; default case if none match
)

Usage:

(match [< 5.0] ...)

should arrange to execute expr2 at runtime.

I would like to write a macro, but I am not sure of the expansion.

I am considering having each case-and-clause expand to a let with bindings to internal variables and checks that the literal symbols ('<) actually matches the pattern. Maybe for the second pattern (['< ub]):

(let [[sym1 ub] pattern]
  (if (= '< sym1)
    expr1)

Do I need to use (gensym) for the bindings? How?

Bigger picture:

(range-case target
            [0.0 < 1.0] :greatly-disagree
            [< 2.0]     :disagree
            [< 3.0]     :neutral
            [< 4.0]     :agree
            [5.0]       :strongly-agree
            42          :the-answer
            :else       :do-not-care)

I am trying to match the [...] patterns and convert them to the following:

[ub]          (if previous-ub `(and (<= ~previous-ub ~target) (<= ~target ~ub))
                              `(< ~target ~ub))
['< ub]       (if previous-ub `(and (<= ~previous-ub ~target) (< ~target ~ub))
                              `(< ~target ~ub))
[lb ub]       `(and (<= ~lb ~target) (<= ~target ~ub))
['< lb ub]    `(and (< ~lb ~target) (<= ~target ~ub))
[lb '< ub]    `(and (<= ~lb ~target) (< ~target ~ub))
['< lb '< ub] `(and (< ~lb ~target) (< ~target ~ub))

I have a cond that checks that the case part is a vector. This pattern match should occur inside that case.

  • 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-23T07:00:02+00:00Added an answer on May 23, 2026 at 7:00 am

    My first idea was basically the same: Bind stuff to internal locals and test on their contents in a big and. For literals the value is bound to a generated local; symbols are used directly in the binding.

    I also added a check that the spec vector matches the length of the target vector. Otherwise you can’t have [ub] as well as [lb ub] since neither contains a check which could fail. So always the first would be selected.

    Here is the code:

    (defn make-clause
      [expr-g [spec expr & more :as clause]]
      (when (seq clause)
        (let [tests-and-bindings (map (fn [x]
                                        (if-not (symbol? x)
                                          (let [x-g (gensym "x")]
                                            [`(= ~x ~x-g) x-g])
                                          [nil x]))
                                      spec)
              tests    (keep first tests-and-bindings)
              bindings (map second tests-and-bindings)]
          `(let [[~@bindings] ~expr-g]
             (if (and (= (count ~expr-g) ~(count spec)) ~@tests)
               ~expr
               ~(make-clause expr-g more))))))
    
    (defmacro match
      [expr & clauses]
      (let [expr-g  (gensym "expr")]
        `(let ~[expr-g expr]
           ~(make-clause expr-g clauses))))
    

    And an example expansion. I didn’t use syntax-quote in the example to reduce the noise in the expansion, but you should get the idea.

    (let [expr98 [(quote <) 3.0]]
      (let [[ub] expr98]
        (if (and (= (count expr98) 1))
          (if previous-ub
            (and (<= previous-ub target) (<= target ub))
            (< target ub))
          (let [[x99 ub] expr98]
            (if (and (= (count expr98) 2) (= (quote <) x99))
              (if previous-ub
                (and (<= previous-ub target) (< target ub))
                (< target ub))
              (let [[lb ub] expr98]
                (if (and (= (count expr98) 2))
                  (and (<= lb target) (<= target ub))
                  nil)))))))
    

    The invokation was:

    (match ['< 3.0]
      [ub]    (if previous-ub
                (and (<= previous-ub target) (<= target ub))
                (< target ub))
      ['< ub] (if previous-ub
                (and (<= previous-ub target) (< target ub))
                (< target ub))
      [lb ub] (and (<= lb target) (<= target ub))))
    

    Hope that helps you get started.

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

Sidebar

Related Questions

So this is a follow-up to a previous question that I asked: Trying to
This is a follow up question to my previous question . I am trying
This is a follow up question to my previous question . I am trying
This is a follow-up question of my previous post . I am trying to
Following on from my previous question about tables I am trying to create additional
I'm trying to follow up on a previous Stackoverflow question about how to display
As a follow-up to this previous question , I'd like to add a title
This question is a follow up to a previous question I had I'm trying
to follow my previous question: XDoclet, a dead tool? what tool can I use
This question is a follow up to my previous question about getting the HTML

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.