6 Replies - 1025 Views - Last Post: 18 April 2015 - 01:40 PM

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Scheme: Removing a char from a string

Posted 18 April 2015 - 11:58 AM

I am trying to remove a certain character from a string input, which is coverted into seperate chars by the built in string->list function, but I keep getting an error:

car: contract violation
expected: pair?
given: '()


Here is my input and code:

input
(delete(string->list "h i"))


code
(define delete
  (lambda (input)
    (cond
      (equal? (car input) #\space  (delete(cdr input)));remove any empty spaces
      (else (cons ((car input)) (delete(cdr input))))
      )
    )
  )


I dont understand why Im getting this error since if I try doing
(car (string->list "hello"))
it works fine, even though it is still a list of chars, returning #\h.

This post has been edited by streek405: 18 April 2015 - 12:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Scheme: Removing a char from a string

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

Re: Scheme: Removing a char from a string

Posted 18 April 2015 - 12:06 PM

car and cdr produce an error when you try to call them on the empty list, which is why you get the error you do. Note that if they didn't do this, you'd get infinite recursion as you don't have a base case.

What you need to do is to add a case to your cond for when the list is empty.
Was This Post Helpful? 1
  • +
  • -

#3 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Scheme: Removing a char from a string

Posted 18 April 2015 - 12:11 PM

View Postsepp2k, on 18 April 2015 - 12:06 PM, said:

car and cdr produce an error when you try to call them on the empty list, which is why you get the error you do. Note that if they didn't do this, you'd get infinite recursion as you don't have a base case.

What you need to do is to add a case to your cond for when the list is empty.

Thank you! I had a feeling that I needed my base case to be that but my friend told me that the first condition covered it...I guess sometimes you really do gotta trust your gut lol.

I added this and it got rid of that error:
(equal? (car input) null input)

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

Re: Scheme: Removing a char from a string

Posted 18 April 2015 - 12:25 PM

Oh, I just realized, the syntax you use in your cond is wrong. Each case of the cond should be an opening parenthesis, followed by a condition, followed by one or more expressions that should be executed when the condition is met (as in not #f), followed by a closing parenthesis.

So when you write (equal? (car input) #\space (delete(cdr input))), equal? is your condition and (car input) #\space (delete(cdr input)) are the expressions to execute when that condition is met. Since equal? is a function, it will never be #f and the condition will always be met. What you want is to call the function, meaning you need an extra pair of parentheses enclosing the function and its arguments.

Once you fix that for both your conditions, you'll probably realize that the condition (equal? (car input) null) is never met. That is because (car input) either returns an element of the list (which will be a character and thus never equal to null or causes an error (if the list was empty). To check whether the list is empty, don't use car on it, just compare it to null directly or, better, use null?.
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10715
  • View blog
  • Posts: 18,351
  • Joined: 19-March 11

Re: Scheme: Removing a char from a string

Posted 18 April 2015 - 12:29 PM

View Poststreek405, on 18 April 2015 - 02:11 PM, said:

Thank you! I had a feeling that I needed my base case to be that but my friend told me that the first condition covered it...I guess sometimes you really do gotta trust your gut lol.


Don't trust your gut, and don't trust your friend's gut. Trust the code. The code is doing what you tell it to. Look at what you're telling it to do, and what that really means, and figure out how to express your actual desires more clearly.
This is why I always tell you to work it out on a whiteboard: if you walk through the situation in your mind, you will see these things.
Was This Post Helpful? 0
  • +
  • -

#6 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Scheme: Removing a char from a string

Posted 18 April 2015 - 01:16 PM

View Postjon.kiparsky, on 18 April 2015 - 12:29 PM, said:

This is why I always tell you to work it out on a whiteboard: if you walk through the situation in your mind, you will see these things.


I agree. I actually do like writing code on the board in my club because it forces you to understand everything step by step, rather than relying on the compiler and auto-fill.

I dont mean to sound mean or anything but there are some people in my CS club, who are in upper division CS classes, who cant write a simple method correctly on the board. I am not saying that I am a pro at coding or anything -- I still have a lot to learn -- but whenever I see stuff like that, for someone at that level, three things come to mind:

1) they are one of those people in my classes who has cheated their way this far into the major
2) they have no idea how to code something without looking at resources all the time
3) they are too nervous to write code in front of other people and cant think at the moment

This post has been edited by streek405: 18 April 2015 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#7 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Scheme: Removing a char from a string

Posted 18 April 2015 - 01:40 PM

View Postsepp2k, on 18 April 2015 - 12:25 PM, said:



Thanks, I'm still not used to the whole syntax of Scheme, since I never really review it that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1