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:02 am]
Oh_Man -- whereas just "press X to get 50 health back" is pretty mindless
[05:02 am]
Oh_Man -- because it adds anotherr level of player decision-making where u dont wanna walk too far away from the medic or u lose healing value
[05:01 am]
Oh_Man -- initially I thought it was weird why is he still using the basic pre-EUD medic healing system, but it's actually genius
[2024-5-06. : 3:04 am]
Ultraviolet -- Vrael
Vrael shouted: I almost had a heart attack just thinking about calculating all the offsets it would take to do that kind of stuff
With the modern EUD editors, I don't think they're calculating nearly as many offsets as you might imagine. Still some fancy ass work that I'm sure took a ton of effort
[2024-5-06. : 12:51 am]
Oh_Man -- definitely EUD
[2024-5-05. : 9:35 pm]
Vrael -- I almost had a heart attack just thinking about calculating all the offsets it would take to do that kind of stuff
[2024-5-05. : 9:35 pm]
Vrael -- that is insane
[2024-5-05. : 9:35 pm]
Vrael -- damn is that all EUD effects?
[2024-5-04. : 10:53 pm]
Oh_Man -- https://youtu.be/MHOZptE-_-c are yall seeing this map? it's insane
[2024-5-04. : 1:05 am]
Vrael -- I won't stand for people going around saying things like im not a total madman
Please log in to shout.


Members Online: Roy