Scheme Help
Mar 29 2008, 3:47 pm
By: Centreri  

Mar 29 2008, 3:47 pm Centreri Post #1

Relatively ancient and inactive

Well, basically, we're supposed to create custom number systems in Scheme. Which I think is completely pointless, but no one cares, so.. Well, anyway, I chose to do a number system with positive/negative integer complex numbers. Basically, we're supposed to create addition, subtraction, quotient and multiplication functions, as well as one of the more interesting functions like sqrt or exp. If someone could help me troubleshoot this, it'd be great. Scheme's not helping at all.
Code
(define add   ;(# #)
 (lambda (a b)
   (cond
         ((and (equal? (car a) 0) (equal? (car (cdr a)) 0)) b)
         ((and (equal? (car b) 0) (equal? (car (cdr b)) 0)) a)
         (else (add ((cond ((> (car b) 0) (+ 1 (car a)))
                           ((equal? (car b) 0) (car a))
                           (else (- (car a) 1)))
                     (cond ((> (car (cdr b)) 0) (+ (car (cdr a)) 1))
                           ((equal? (car (cdr b)) 0) (car (cdr a)))
                           (else (- (car (cdr a)) 1))))
                    ((cond ((> (car b) 0)(- (car b) 1))
                           ((equal? (car b) 0) (car b))
                           (else (+ (car b) 1)))
                     (cond ((> (car (cdr b)) 0)(- (car (cdr b)) 1))
                           ((equal? (car (cdr b)) 0)(car (cdr b)))
                           (else (+ (car (cdr b)) 1)))))))))
         
'
This is the adding function (from which I can probably derive everything else, or at least learn from it). It doesn't work. The complex numbers are supposed to go like this: (# #). The first one is a positive/negative real number, the second is positive/negative imaginary number. What I try to do here is basically create a recursive loop that keeps either increasing/decreasing the b list to (0 0) While increasing/decreasing the A list accordingly.

Any help much appreciated :).



None.

Mar 29 2008, 7:03 pm fatimid08 Post #2



I can give you a pointer on how NOT to do your calculator:
Calculators gone bad

Can you do bitwise operations in Scheme? That could help (although I'm not sure if the way I'm thinking it could be implemented isn't a huge wtf by itself) Or bitshift, although I don't quite see how to implement that off top of my head.



None.

Mar 29 2008, 7:41 pm Doodle77 Post #3



You've probably misplaced a parenthesis :P
Expansion:
Code
(define add   ;(# #)
(lambda (a b)
   (cond
     ((and (equal? (car a) 0) (equal? (car (cdr a)) 0)) b)
     ((and (equal? (car b) 0) (equal? (car (cdr b)) 0)) a)
     (else
       (add
         (
         (cond ((> (car b) 0) (+ 1 (car a)))
           ((equal? (car b) 0) (car a))
           (else (- (car a) 1)))
     
         (cond ((> (car (cdr b)) 0) (+ (car (cdr a)) 1))
           ((equal? (car (cdr b)) 0) (car (cdr a)))
           (else (- (car (cdr a)) 1)))
         )
         (
         (cond ((> (car b) 0)(- (car b) 1))
           ((equal? (car b) 0) (car b))
           (else (+ (car b) 1)))
         (cond ((> (car (cdr b)) 0)(- (car (cdr b)) 1))
           ((equal? (car (cdr b)) 0)(car (cdr b)))
           (else (+ (car (cdr b)) 1)))
         )
       )
     )
   )
 )
)


Post has been edited 5 time(s), last time on Mar 29 2008, 7:51 pm by Doodle77.



None.

Mar 29 2008, 9:08 pm Centreri Post #4

Relatively ancient and inactive

Produces errors.
Quote
(add (3 4) (3 4))
{bug} procedure application: expected procedure, given: 3; arguments were: 4

With the most unhelpful error message ever.

Quote
Can you do bitwise operations in Scheme? That could help (although I'm not sure if the way I'm thinking it could be implemented isn't a huge wtf by itself) Or bitshift, although I don't quite see how to implement that off top of my head.
Scheme's a bit unorthadox a language. We can't use bitwise operations or anything like that - just basics of what you learn in two months of Scheme in a classroom of 30 students.

Post has been edited 1 time(s), last time on Mar 29 2008, 9:18 pm by Centreri.



None.

Mar 29 2008, 9:27 pm Centreri Post #5

Relatively ancient and inactive

Jesus, navigating through those mounds of parenthases and succeeding must make me a genius. My successful answer:
Code
(define add   ;(# #)
 (lambda (a b)
   (cond
         ((and (equal? (car a) 0) (equal? (car (cdr a)) 0)) b)
         ((and (equal? (car b) 0) (equal? (car (cdr b)) 0)) a)
         (else (add (cons (cond ((> (car b) 0) (+ 1 (car a)))
                                ((equal? (car b) 0) (car a))
                                (else (- (car a) 1)))
                    (cons (cond ((> (car (cdr b)) 0) (+ (car (cdr a)) 1))
                                ((equal? (car (cdr b)) 0) (car (cdr a)))
                                (else (- (car (cdr a)) 1))) '()))
                    (cons (cond ((> (car b) 0)(- (car b) 1))
                                ((equal? (car b) 0) (car b))
                                (else (+ (car b) 1)))
                    (cons (cond ((> (car (cdr b)) 0)(- (car (cdr b)) 1))
                                ((equal? (car (cdr b)) 0)(car (cdr b)))
                                (else (+ (car (cdr b)) 1))) '())))))))

This thread is now dedicated to asking Scheme-related questions and praising my awesomeness. Mostly praising my awesomeness. Let the 1337/\/355 commence.

Updates: Things started moving really quickly with add out of the way. Here come Subtract and Multiply!
Code
(define subtract
 (lambda (a b)
   (add a (cons (- 0 (car b)) (cons (- 0 (car (cdr b))) '())))))

(define multiply
 (lambda (a b)
   (cons (+ (* (car a) (car b)) (* -1 (car (cdr a)) (car (cdr b))))
         (cons (+ (* (car a) (car (cdr b))) (* (car (cdr a)) (car b))) '()))))


EDIT: I feel 100% retarded. I went through all the work making the Add function, when this would suffice...
Code
(define add2 ;I felt so stupid making the first function after and realizing I could've done it like this...
 (lambda (a b)
    (cons (+ (car a) (car b)) (cons (+ (car (cdr a)) (car (cdr b))) '()))))


Post has been edited 2 time(s), last time on Mar 29 2008, 10:11 pm by Centreri.



None.

Mar 29 2008, 10:05 pm fatimid08 Post #6



Gah all those parenthesis (is the plural the same as the singular?) hurt my eyes. Why do you even have to learn Scheme in class? Heck, implementing a math system looks easier in SC triggers than in Scheme.



None.

Mar 29 2008, 10:11 pm Centreri Post #7

Relatively ancient and inactive

I'm very sad. Check my 2nd edit. I spent so much time on that Add function, when it was completely obsolete. Well, I might get a few extra points for it :P.



None.

Mar 29 2008, 10:19 pm fatimid08 Post #8



You should have worsened it for the next OMGWTF contest over at thedailywtf.com, or for the corrector's pleasure at looking at a horror which works (leave the simpler version as some "beta" work or something and never call it).



None.

Mar 29 2008, 10:21 pm Centreri Post #9

Relatively ancient and inactive

Worsened it? Don't make me do it -.-.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[05:47 pm]
NimoStar -- If its any consolation, the human brain would still mop the floor with this bot if it wasn't limited by the mechanical interface
[05:34 pm]
NimoStar -- Well. The bot didn't come up with any new strategy. It just has superhuman micro. Hard to beat that. I reckon even the simplest dedicated chess engines are unbeatable for humans now.
[02:13 pm]
Oh_Man -- all g
[10:54 am]
NudeRaider -- Oh_Man
Oh_Man shouted: NudeRaider did u watch the entire video? it's not one game. it's currently dominating the ladder at number 1 position. do ur research first!
Oops, caught me there, no I haven't. :( I was imagining how the game went. Sorry for that. Watching it now. brb
[10:52 am]
NudeRaider -- NimoStar
NimoStar shouted: They were not "Unprepared" that was their fifth loss in a row against the bot. The other replays are also available
that doesn't mean he was prepared. When humans come around with new strategies it often takes weeks until the pros figure out how to shut it down. Oh_Man that's certainly a way more convincing argument for micro being "too good to beat".
[09:43 am]
DiearAnother -- wirefram
[08:35 am]
NimoStar -- Well, pros showed that long ago... half of zerg pros just ling muta rush
[08:35 am]
NimoStar -- " its shown is that you can trade 1000's of apm to make up for lack of strategy"
[03:25 am]
Oh_Man -- https://youtu.be/R_6bsf9REkY anyone ever tried playing SC ghost?
Please log in to shout.


Members Online: Symmetry