scheme
Zephyr_Pure wrote: It's silly to ask if a language is worth learning… If it wasn't worth learning, then no one would learn it, and the language would die. It's better to ask if a language is worth learning when you have a specific goal in mind.
The next question would be: what is your goal? What are you striving for? What is it that needs achievement? Tell us and we can think this thing through better, more accurate if you wish.
bonzibuddy wrote: ok, the question i should have asked is what is the most common use of scheme?. also what are its advantages over other languages? i was just interested. at this time i dont really have a purpouse for it other than to gain knowledge in programming.
if you want to know about something like that, google is the easiest way to find out. here's a bit of info here.
the primary use that i've seen for scheme is teaching the concepts of programming. The language is very helpful if you want to understand how lists, iteration, recursion, etc are really working. if you want to read up a bit before you decide, i have two articles posted here on HBH.
scheme is a functional recursive language. basically uses all functions and recursion is crazy… u use it for like AI programming and what not.. its not really used for software development at all…. so if your not developing an AI or something then don't bother using it . plus its hard to wrap your head around.
(DEFINE s NULL) (DEFINE p NULL)
;; Discards spaces, commas, and semi-colons and Detects and removes end of line
(DEFINE (eatspace)
(let ((ch (peek-char)))
(cond ((or (eof-object? ch) (char=? #\newline ch)) #f)
((not (or (char=? ch #\,)
(char-whitespace? ch)
(char=? ch #\;)))#t)
(else (begin (read-char) (eatspace))))))
;; Reads a line, discarding commas and semi-colons.
(DEFINE (mygetline)
(if (eatspace)
(let ((word (read-char)))
(cons word (mygetline)))
(begin (read-char) '())))
;; Reads n lines, putting each in its own list.
(DEFINE (getlines n)
(let ((L (mygetline)))
(if (= n 0) L (cons L (getlines (- n 1))))))
(DEFINE L (mygetline))
;––––––––––––––––––––––––––––––––––––––;
; NAME: infix_to_postfix ;
; PURPOSE: distributes the elements into the correct lists ;
; INPUT: infix expression and 2 empty lists ;
; OUTLINE: if the expression entered is NULL it exits, otherwise it reads in ;
; elements into the correct lists. Numbers into postfix list and ;
; operators and the open bracket onto the stack. If it is a closed ;
; bracket, it calls a function. If a invalid value is inputed, the ;
; program will warn the user. ;
; OTHER FUNCTIONS: check, end_bracket ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (infix_to_postfix expr postfix stack)
(if(not(NULL? expr))
(COND
((EQ? (CAR expr) '#\1)(check expr (CONS 1 postfix) stack))
((EQ? (CAR expr) '#\2)(check expr (CONS 2 postfix) stack))
((EQ? (CAR expr) '#\3)(check expr (CONS 3 postfix) stack))
((EQ? (CAR expr) '#\4)(check expr (CONS 4 postfix) stack))
((EQ? (CAR expr) '#\5)(check expr (CONS 5 postfix) stack))
((EQ? (CAR expr) '#\6)(check expr (CONS 6 postfix) stack))
((EQ? (CAR expr) '#\7)(check expr (CONS 7 postfix) stack))
((EQ? (CAR expr) '#\8)(check expr (CONS 8 postfix) stack))
((EQ? (CAR expr) '#\9)(check expr (CONS 9 postfix) stack))
((EQ? (CAR expr) '#\0)(check expr (CONS 0 postfix) stack))
((EQ? (CAR expr) '#\()(check expr postfix (CONS '#\( stack)))
((EQ? (CAR expr) '#\+)(check expr postfix (CONS '#\+ stack)))
((EQ? (CAR expr) '#\-)(check expr postfix (CONS '#\- stack)))
((EQ? (CAR expr) '#\)(check expr postfix (CONS '#\ stack)))
((EQ? (CAR expr) '#\/)(check expr postfix (CONS '#\/ stack)))
((EQ? (CAR expr) '#\))(end_bracket expr postfix stack))
(ELSE (DISPLAY "Only numbers 0 to 9 and +,-,,/ can be used in the equation!"))
)))
;––––––––––––––––––––––––––––––––––––––;
; NAME: check ;
; PURPOSE: checks to see if numbers 0 to 9, any math ops or open brackets ;
; are at the end of the inputed infix expression. ;
; INPUT: expr, postfix, stack ;
; OUTLINE: if the expression does not end with a closing bracket, program ;
; displays that equation is invalid, otherwise continue on… ;
; OTHER FUNCTIONS: infix_to_postfix ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (check expr postfix stack)
(if(NULL? (CDR expr))(DISPLAY "Invalid Equation!"))
(if(not(NULL?(CDR expr)))(infix_to_postfix (CDR expr)postfix stack))
)
;––––––––––––––––––––––––––––––––––––––;
; NAME: end_bracket ;
; PURPOSE: removes brackets from stack if an open bracket follows the closed ;
; bracket. ;
; INPUT: expr, postfix, stack ;
; OUTLINE: if a math op isn't on the top of the stack, then the equation is ;
; invalid. Otherwise continues on to clean_stack function. ;
; OTHER FUNCTIONS: clean_stack ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (end_bracket expr postfix stack)
(if(not(NULL? stack))
(COND
((EQ? (CAR stack) '#\+)(clean_stack (CDR expr) (CONS '#\+ postfix) (CONS '#\)(CDR stack))))
((EQ? (CAR stack) '#\-)(clean_stack (CDR expr) (CONS '#\- postfix) (CONS '#\)(CDR stack))))
((EQ? (CAR stack) '#\)(clean_stack (CDR expr) (CONS '#\* postfix) (CONS '#\)(CDR stack))))
((EQ? (CAR stack) '#\/)(clean_stack (CDR expr) (CONS '#\/ postfix) (CONS '#\)(CDR stack))))
(ELSE (DISPLAY "Invalid Equation!"))
))
(if(NULL? stack)(DISPLAY "Invalid Equation!"))
)
;––––––––––––––––––––––––––––––––––––––;
; NAME: clean_stack ;
; PURPOSE: cleans stack of open and closed brackets next to each other ;
; INPUT: expr, postfix, stack ;
; OUTLINE: if the second element in the stack is a open bracket then call ;
; check2 with these 2 elements removed. Else it is an invalid ;
; equation. ;
; OTHER FUNCTIONS: check2 ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (clean_stack expr postfix stack)
(COND
((EQ? (CAR(CDR stack)) '#\( ) (check2 expr postfix (CDR(CDR stack))))
(ELSE (DISPLAY "Invalid Equation!"))
))
;––––––––––––––––––––––––––––––––––––––;
; NAME: check2 ;
; PURPOSE: controls the infix_to_postfix from evaluating an empty list. ;
; INPUT: expr, postfix, stack ;
; OUTLINE: if the infix expression is empty, then call swap to swap and ;
; display the postfix and answer. Otherwise continue evaluating ;
; the infix expression. ;
; OTHER FUNCTIONS: swap, infix_to_postfix ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (check2 expr postfix stack)
(COND
((EQ? expr NULL)
(if(NULL? stack)(swap postfix NULL)))
(ELSE (infix_to_postfix expr postfix stack))
))
;––––––––––––––––––––––––––––––––––––––;
; NAME: swap ;
; PURPOSE: reverses the backwards postfix so it can be printed correctly ;
; INPUT: original postfix and an empty list to hold new postfix ;
; OUTLINE: as long as the list isn't empty, the CAR of the list is placed on ;
; the post list. Once list is NULL, the postfix expr is evaluated ;
; OTHER FUNCTIONS: evaluate_postfix ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (swap list post)
(COND
((EQ? list NULL)(DISPLAY "Postfix: ")(print post)(evaluate_postfix post NULL))
(ELSE (swap (CDR list)(CONS (CAR list) post)))
))
;––––––––––––––––––––––––––––––––––––––;
; NAME: print ;
; PURPOSE: print only the values, not the brackets of the list ;
; INPUT: a list from another function ;
; OUTLINE: prints each element to screen with a space untile the list is NULL;
; OTHER FUNCTIONS: <none> ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (print list)
(COND
((not(NULL? list))(DISPLAY (CAR list))(DISPLAY '#\ )(print(CDR list))))
)
;––––––––––––––––––––––––––––––––––––––;
; NAME: evaluate_postfix ;
; PURPOSE: controls the infix_to_postfix from evaluating an empty list. ;
; INPUT: postfix list and an empty stack to store the answer ;
; OUTLINE: calls function until the post list is NULL. Any numbers are ;
; added to the empty list. When an op is found, it performs the op ;
; to the values in the list. If there is only 1 value when a list ;
; needs to be applied, the program lets the user know that the eqn ;
; was invalid. When done, it calls a function to display the answer;
; OTHER FUNCTIONS: output_answer ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (evaluate_postfix post a)
(if(not(NULL? post))
(COND
((EQ? (CAR post) '#\+)
(if(NULL?(CDR a))(DISPLAY "Invalid Equation!"))
(if(not(NULL?(CDR a)))
(evaluate_postfix (CDR post) (CONS(+(CAR(CDR a))(CAR a))(CDR(CDR a))))))
((EQ? (CAR post) '#\-)
(if(NULL?(CDR a))(DISPLAY "Invalid Equation!"))
(if(not(NULL?(CDR a)))
(evaluate_postfix (CDR post) (CONS(-(CAR(CDR a))(CAR a))(CDR(CDR a))))))
((EQ? (CAR post) '#\)
(if(NULL?(CDR a))(DISPLAY "Invalid Equation!"))
(if(not(NULL?(CDR a)))
(evaluate_postfix (CDR post) (CONS((CAR(CDR a))(CAR a))(CDR(CDR a))))))
((EQ? (CAR post) '#\/)
(if(NULL?(CDR a))(DISPLAY "Invalid Equation!"))
(if(not(NULL?(CDR a)))
(evaluate_postfix (CDR post) (CONS(/(CAR(CDR a))(CAR a))(CDR(CDR a))))))
(ELSE (evaluate_postfix (CDR post)(CONS (CAR post)a)))
))(if(NULL? post)(output_answer a))
)
;––––––––––––––––––––––––––––––––––––––;
; NAME: output_answer ;
; PURPOSE: prints the answer to the equation ;
; INPUT: the answer ;
; OUTLINE: prints the answer to the screen ;
; OTHER FUNCTIONS: <none> ;
;––––––––––––––––––––––––––––––––––––––;
(DEFINE (output_answer a)
(NEWLINE)(DISPLAY "Answer: ")(print a)
)
;edit by spyware: next time disable smilies yourself, thanks.
it's probably better to look at simple programs first:
(cond ((null? set) #f)
((= x (car set)) #t)
(else (element? (cdr set)))))
(define (union set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
((element? (car set1) set2) (union (cdr set1) set2)) ;; comment this line to allow duplicates
(else (cons (car set1) (union (cdr set1) set2)))))
element? takes in a value and a list and checks to see if the value is in the list. union takes two lists and combines them. i think they're built in functions.