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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:23:48+00:00 2026-05-27T03:23:48+00:00

I naively implemented the trie from Okasaki’s book Purely Functional Data Structures in Clojure.

  • 0

I naively implemented the trie from Okasaki’s book “Purely Functional Data Structures” in Clojure. Just for learning purposes, because probably there is no real gain, since Clojure used a more efficient trie under the hood of normal maps, right? There is one thing that still bothers me about it: trie-assoc doesn’t use recur. How could I refactor it so it only does a recursive call on the tail using recur? I have the feeling I could use zippers for this, but I haven’t looked into zippers that much yet.
One other thing that maybe could be improved is the (if (empty? key) …) followed by a destructuring on key. Could this be more concise?

(ns okasaki-trie.core)

(def empty-trie {:value nil :nodes nil})

(defn trie-get [key trie]
  (if (empty? key) (get trie :value)
      (let [[k & ks] key]
        (recur ks (get-in trie [:nodes k])))))

(defn trie-assoc [key val trie]
  (if (empty? key) (assoc trie :value val) 
      (let [[k & ks] key
            sub-trie-k (or (get-in trie [:nodes k]) empty-trie)
            sub-trie-k-new (trie-assoc ks val sub-trie-k)]
        (assoc-in trie [:nodes k] sub-trie-k-new))))

;;(def first-trie (trie-assoc "a" 3 empty-trie))
;;first-trie => {:nodes {\a {:nodes nil, :value 3}}, :value nil}

;;(def second-trie (trie-assoc "ab" 5 first-trie))
;;second-trie => {:nodes {\a {:nodes {\b {:nodes nil, :value 5}},
;;:value 3}}, :value nil}

;;(def third-trie (trie-assoc "b" 2 second-trie))
;;third-trie  => {:nodes {\b {:nodes nil, :value 2}, \a {:nodes {\b
;;{:nodes nil, :value 5}}, :value 3}}, :value nil}

;;(trie-get "b" third-trie)  ;=> 2
;;(trie-get "ab" third-trie) ;=> 5
;;(trie-get "abc" third-trie)  ;=> nil
  • 1 1 Answer
  • 3 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-27T03:23:49+00:00Added an answer on May 27, 2026 at 3:23 am

    I haven’t looked deeply into this algorithm, but I doubt if tail recursion is (usefully) possible. You have to walk down a tree, and then back up it to reconstruct the nodes on the path to the root. This requires a stack, hence no recur.

    If you want, you can manage your own “stack” on the heap, by keeping a list of parent-nodes and manually walking them back to the root. Then you can use recur and avoid stack overflows, but you won’t be using any less memory (probably more, really). So this is only a useful trick if you have trees with 2^1000 elements in them (and I rather doubt that you do).

    Edit

    Just noticed your question about

    (if (empty? key)
      (...empty...)
      (let [[k & ks] key]
        (...more...)))
    

    You can rewrite this more simply and concisely as:

    (if-let [[k & ks] (seq key)]
      (...more...)
      (...empty...))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that receives a pointer to JPEG data from a camera
Perhaps naively, I created a class (AdminDatabase) to handle connection to different MS-Access database
Coding footer naively, if there's not enough content, then there will be empty space
I have just heard that the iphone cannot do double natively thereby making them
I have a UIWebView with content populated from a Last.fm API call. This content
this is just a quick probe to see if this is technically possible. I'm
I have implemented language localization to an Android application. My questions are: I can't
Flash 10+ allows peer to peer capabilities to be implemented in Flex and Flash
If our organisation were to switch from a central-server VCS like subversion to a
Just a very general question, that not only applies to this example. Let's say

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.