3 Replies - 667 Views - Last Post: 15 August 2010 - 03:56 PM

#1 apd3691  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 29-July 10

Clojure: Not returning the right value.

Posted 14 August 2010 - 02:19 PM

I'm currently learning Clojure on my own and thought I would try to make a function to check if a string is a palindrome. The program looks like this:

(defn palindrome? [string]
  ;This function checks to see if string is a palindrome
  (let [length (.length string)] ;Get length of the string
    (if (or (= length 1) (= length 0)) ;Base case: if string is size 1 or 0, it's a palindrome
      true)
    (if (= (.charAt string 0) (.charAt string (- length 1))) ;Otherwise check the back and front and see if they match
        (palindrome? (.substring string 1 (- length 2))) ;Recursively check the front and back letters
        false))) ;Otherwise if they don't match, it's not a palindrome

;This code just makes a prompt and allows the user to input a string
;prints 'Yes' if it is a palindrome
;otherwise it prints 'No'
(print "=>")
(flush)
(if (is_palindrome (str (read))) 
  (println "Yes")
  (println "No"))



When I put in the string "HellolleH" it prints 'No'. Can anyone point me into the right direction? Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Clojure: Not returning the right value.

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Clojure: Not returning the right value.

Posted 14 August 2010 - 03:03 PM

I'm actually having trouble figuring out exactly what is going on in that code. This is grossly over-complicating a very uncomplicated task.

The first thing you need to do is completely delete your palindrome function and start anew. You're going about it the wrong way.

Let's look at exactly what a palindrome is. A palindrome is a string that is equal to itself reversed.

In your REPL, do this: (doc reverse).

Reverse takes a sequence, reverses it, and then returns it. You'll notice that it doesn't appear as it normally would once it's reversed.

user> (reverse "blah")
(\h \a \l \B)/>



This is because reverse doesn't work on strings specifically, but works on any sequence, and returns a sequence representation of the string. You can make it look like a string again by using apply and str.

user> (apply str (reverse "blah"))
"halb"



The str takes any number of arguments and concatenates them all into one big string. However, if you call str directly on the sequence returned by (reverse "blah), it'll be a single argument, and it'll give you a string representation of that sequence, which isn't helpful. apply takes a function and a sequence and it applies that function to all of the elements of the sequence. It basically unrolls the sequence.

(apply str ["blah" "blah" "blah"]) is the same as (str "blah" "blah" "blah")


Do you see how to do what you want to do yet? Remember. A palindrome is a string that is equal to the reverse of that string. I'll leave the completed code here as a spoiler. You only have to look at it if you can't figure it out yourself.

Spoiler


EDIT: Oh, I forgot to mention that Clojure has a count function. Do (doc count) in your REPL. I noticed you were using .length. Also, (doc nth) rather than charAt.

user> (nth "blah blah blah" 0)
\b



You rarely have to resort to Java functions for working with sequences. It's generally better to use what Clojure offers, because they're optimized and designed for Clojure sequences. It's perfectly understandable when you don't actually know about these functions yet though. Check out the find-doc function. (doc find-doc), you can use it to find functions and stuff. There is also the Clojure API documentation, and ME of course. :P

This post has been edited by Raynes: 14 August 2010 - 03:08 PM

Was This Post Helpful? 2
  • +
  • -

#3 apd3691  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 29-July 10

Re: Clojure: Not returning the right value.

Posted 15 August 2010 - 12:04 PM

Thank you for the advice Raynes. I'm pretty new to these functional languages (my background is C++). I'll try out what you said and see where that goes. Thank you. :smile2:

EDIT: I reworked my code and it looks like yours actually only I used a temporary variable :)

(defn palindrome? [text]
  (let [reverse_str (apply str (reverse text))]
    (= reverse_str text)))



Thank you for the help.

This post has been edited by apd3691: 15 August 2010 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Clojure: Not returning the right value.

Posted 15 August 2010 - 03:56 PM

You're welcome.

On a only partially related note, I noticed you named local value "reverse_str". Instead, name it "reverse-str". Clojure uses a convention where words are separated by hyphens rather than underscores.

Nice work! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1