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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:19:02+00:00 2026-06-04T13:19:02+00:00

I want to turn a defined function into an anonymous one. How do I

  • 0

I want to turn a defined function into an anonymous one. How do I do that? The following function returns the last element from a sequence:

(defn lastt [l]
    (cond
        (nil? (next l)) l
        :else
            (lastt (next l))))

How do I turn it into fn form?

PS: I know about last function, this is just an exercise.

  • 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-04T13:19:03+00:00Added an answer on June 4, 2026 at 1:19 pm

    First of all, that function returns a list with the last item in it. I’d change your definition so that it returns just the last item:

    (defn lastt [l]
      (cond
       (nil? (next l)) (first l)
       :else (lastt (next l))))
    

    To simplify, I’d use a let binding since you’re calling next twice on l:

    (defn lastt [l]
      (let [n (next l)]
        (cond
         (nil? n) (first l)
         :else (lastt n))))
    

    The next thing I’d do is replace the final call to lastt to use recur instead

    (defn lastt [l]
      (let [n (next l)]
        (cond
         (nil? n) (first l)
         :else (recur n))))
    

    And then I’d replace that with

    #(let [n (next %)]
       (cond
        (nil? n) (first %)
        :else (recur n)))
    

    And just realised that it could be simplified even more using destructuring 🙂

    #(let [[h & t] %]
       (cond
        (nil? t) h
        :else (recur t)))
    

    Updated

    No need for the cond, since there’s only two branches, and using fn instead of the # shorthand will allow the destructuring to happen in the fn‘s parameters, making the whole function a little bit more concise:

    (fn [[h & t]]
      (if (empty? t) h
          (recur t)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of points that i want to turn into a closed
I have the following array, $myarray[0]['first_name'] $myarray[0]['last_name'] I want to turn it into: $myarray['first_name']
I want to plot a line from one well-defined point to another and then
I have a list of objects which I want to turn into a set.
I created a console application, but I want to turn it into a windows
I want to take a list [0,1,2] and turn it into [0,1,2,2,1,0] . Right
I want to accept a url like this one www.mysite.com/1-2,3 and turn it one
I want to define my own datatype that can hold a single one of
I want to turn something like this CS 240, CS 246, ECE 222, ...
I made an MFC application, and now i want to turn off the window's

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.