diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | basics.scm | 52 | ||||
-rw-r--r-- | hello/hello-test.scm | 20 | ||||
-rwxr-xr-x | hello/hello.scm | 16 | ||||
-rw-r--r-- | number-guesser.scm | 31 | ||||
-rwxr-xr-x | palindrome.scm | 16 |
6 files changed, 136 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/basics.scm b/basics.scm new file mode 100644 index 0000000..4c52883 --- /dev/null +++ b/basics.scm @@ -0,0 +1,52 @@ +;;;; variables +(define my-var 15) +my-var + +;; set to something else +(set! my-var 20) + +;;;; functions +(define (find value lst) + (if (pair? lst) + (begin + (display (string-append "Looking at: " (number->string (car lst)) + "\n")) + (if (equal? (car lst) value) + (begin (display "Found it!\n") + (car lst)) + (find value (cdr lst)))) + #f)) + +(find 5 '(1 2 3 4 5 6)) + +;; note that variables and functions exist in the same +;; namespace, and that we can define functions in a +;; slightly more ugly way, but looks a lot like a var +;; declaration: +(define find-x (lambda (value lst) + (if (pair? lst) + (begin + (display (string-append "Looking at: " (number->string (car lst)) + "\n")) + (if (equal? (car lst) value) + (begin (display "Found it!\n") + (car lst)) + (find value (cdr lst)))) + #f))) + +(find-x 4 '(1 2 3 4 5 6)) + +;;;; let + +;; let as a looping construct +(let find-loop ((value 5) + (lst '(1 2 3 4 5))) + (if (pair? lst) + (begin + (display (string-append "Looking at: " (number->string (car lst)) + "\n")) + (if (equal? (car lst) value) + (begin (display "Found it!\n") + (car lst)) + (find-loop value (cdr lst)))) + #f)) diff --git a/hello/hello-test.scm b/hello/hello-test.scm new file mode 100644 index 0000000..1865112 --- /dev/null +++ b/hello/hello-test.scm @@ -0,0 +1,20 @@ +(use-modules (srfi srfi-64) + (hello)) + +(test-begin "harness") + + +(test-equal "test-hello" + "hello world\n" + (hi)) + +(test-equal "test-hello-bd" + "hello bd\n" + (hi "bd")) + +(test-equal "test-hello-db" + "hello db\n" + (hi "db")) + + +(test-end "harness") diff --git a/hello/hello.scm b/hello/hello.scm new file mode 100755 index 0000000..bf8dfa0 --- /dev/null +++ b/hello/hello.scm @@ -0,0 +1,16 @@ +(define-module (hello)) + +(define GREETING_PREFIX "hello ") +(define GREETING_SUFFIX "\n") +(define DEFAULT_ADDRESSEE "world") + + +(define-public hi + (lambda* (#:optional name) + (string-append GREETING_PREFIX (addressee name) GREETING_SUFFIX))) + +(define addressee + (lambda (name) + (if name + name + DEFAULT_ADDRESSEE))) diff --git a/number-guesser.scm b/number-guesser.scm new file mode 100644 index 0000000..486eea5 --- /dev/null +++ b/number-guesser.scm @@ -0,0 +1,31 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Example game: ;; +;; scheme@(guile-user)> (start 60 70) ;; +;; $21 = 65 ;; +;; scheme@(guile-user)> (bigger) ;; +;; $22 = 68 ;; +;; scheme@(guile-user)> (bigger) ;; +;; $23 = 69 ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; also see (info "(guile) Definition") + +(define lower 1) +(define upper 100) + +(define (start n m) + (set! lower (min n m)) + (set! upper (max n m)) + (guess)) + +;;;; helpers +(define (guess) + (quotient (+ lower upper) 2)) + +(define (smaller) + (set! upper (max lower (- (guess) 1))) + (guess)) + +(define (bigger) + (set! lower (min upper (+ (guess) 1))) + (guess)) diff --git a/palindrome.scm b/palindrome.scm new file mode 100755 index 0000000..788fa06 --- /dev/null +++ b/palindrome.scm @@ -0,0 +1,16 @@ +#!/run/current-system/profile/bin/guile \ +-e main -s +!# + + +(define (main args) + (format #t "String was ~aa palindrome!\n" + (if (palindrome? (cleanup (car (cdr args)))) + "" "not "))) + +(define (cleanup str) + (string-downcase (string-filter + char-set:letter str))) + +(define (palindrome? str) + (equal? str (string-reverse str))) |