Compare commits

...

4 Commits

Author SHA1 Message Date
Bruno Ciccarino λ
07dc44df43
Merge a5e293a7e5 into d9225142d0 2024-12-09 03:49:05 -07:00
Evan Hock
d9225142d0
[haskell/en] Minor changes that were bothering me (#5207) 2024-12-09 03:47:38 -07:00
Boris Verkhovskiy
c8af038bda Update readmes 2024-12-09 03:38:02 -07:00
BrunoCiccarino
a5e293a7e5 feat(main): adding an scheme tutorial 2024-12-07 20:01:15 -03:00
4 changed files with 345 additions and 19 deletions

View File

@ -1,5 +1,5 @@
- [ ] I solemnly swear that this is all original content of which I am the original author
- [ ] Pull request title is prepended with `[language/lang-code]` (example `[python/fr-fr]` or `[java/en]`)
- [ ] Pull request title is prepended with `[language/lang-code]` (example `[python/fr]` or `[java/en]`)
- [ ] Pull request touches only one file (or a set of logically related files with similar changes made)
- [ ] Content changes are aimed at *intermediate to experienced programmers* (this is a poor format for explaining fundamental programming concepts)
- [ ] If you've changed any part of the YAML Frontmatter, make sure it is formatted according to [CONTRIBUTING.md](https://github.com/adambard/learnxinyminutes-docs/blob/master/CONTRIBUTING.markdown)

View File

@ -17,11 +17,10 @@ review them more effectively and/or individually.
## Style Guidelines
* **Keep lines under 80 chars**
* Try to keep **line length in code blocks to 80 characters or fewer**.
* Try to keep line length in code blocks to 80 characters or fewer.
* Otherwise, the text will overflow and look odd.
* This and other potential pitfalls to format the content consistently are
identified by the freely available
[markdownlint](https://github.com/markdownlint/markdownlint).
identified by [markdownlint](https://github.com/markdownlint/markdownlint).
* **Prefer example to exposition**
* Try to use as few words as possible.
* Code examples are preferred over exposition in all cases.
@ -58,17 +57,15 @@ Other fields:
*tool* or *Algorithms & Data Structures*. Defaults to *language* if omitted.
* **filename**: The filename for this article's code. It will be fetched, mashed
together, and made downloadable.
* For non-English articles, *filename* should have a language-specific
suffix.
* **lang**: For translations, the human language this article is in. For
categorization, mostly.
* For non-English articles, *filename* will be used from the English article,
unless you want to overwrite it for some reason.
Here's an example header for an Esperanto translation of Ruby:
Here's an example header for Ruby:
```yaml
*--
language: Ruby
filename: learnruby-epo.ruby
filename: learnruby.rb
contributors:
- ["Doktor Esperanto", "http://example.com/"]
- ["Someone else", "http://someoneelseswebsite.com/"]
@ -77,8 +74,7 @@ contributors:
### Syntax highlighter
[Pygments](https://pygments.org/languages/) is used for syntax highlighting through
[pygments.rb](https://github.com/pygments/pygments.rb).
[Pygments](https://pygments.org/languages/) is used for syntax highlighting.
### Should I add myself as a contributor?
@ -89,12 +85,10 @@ addition or not.
## Building the site locally
Install Ruby. On macOS this can be done with [Homebrew](https://brew.sh/).
Install Python. On macOS this can be done with [Homebrew](https://brew.sh/).
```sh
brew install ruby
# Install Ruby package manager
gem install bundler
brew install python
```
Then clone two repos, install dependencies and run.
@ -107,8 +101,12 @@ git clone https://github.com/<YOUR-USERNAME>/learnxinyminutes-docs ./learnxinymi
# Install dependencies
cd learnxinyminutes-site
bundle install
pip install -r requirements.txt
# Run
bundle exec middleman serve
python build.py
cd build
python -m http.server
# open http://localhost:8000/ in your browser of choice
```

View File

@ -66,6 +66,7 @@ True || False -- True
['H', 'e', 'l', 'l', 'o'] -- "Hello"
-- Lists can be indexed with the `!!` operator followed by an index
-- but this is an O(n) operation because lists are linked lists
"This is a string" !! 0 -- 'T'
@ -273,7 +274,7 @@ case args of
map (*2) [1..5] -- [2, 4, 6, 8, 10]
-- you can make a for function using map
for array func = map func array
for list func = map func list
-- and then use it
for [0..5] $ \i -> show i
@ -281,6 +282,9 @@ for [0..5] $ \i -> show i
-- we could've written that like this too:
for [0..5] show
-- filter keeps only the elements in a list that satisfy a condition
filter even [1..10] -- [2, 4, 8, 10]
-- You can use foldl or foldr to reduce a list
-- foldl <fn> <initial value> <list>
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

324
scheme.html.markdown Normal file
View File

@ -0,0 +1,324 @@
---
language: "Scheme"
filename: scheme.scm
contributors:
- ["Bruno Ciccarino", "https://github.com/BrunoCiccarino"]
---
Scheme is a minimalist dialect of Lisp that is widely used in education, research, and industry. It emphasizes simplicity, powerful abstractions, and functional programming paradigms.
A classic resource to learn Scheme is [Structure and Interpretation of Computer Programs (SICP)](https://web.mit.edu/6.001/6.037/sicp.pdf). For a modern introduction, consider [The Scheme Programming Language](https://www.scheme.org/).
```scheme
;;;-----------------------------------------------------------------------------
;;; 0. Syntax
;;;-----------------------------------------------------------------------------
;;; General form
;;; Scheme has two fundamental elements of syntax: ATOM and S-EXPRESSION.
;;; S-expressions are used for both data and code.
10 ; a number atom; evaluates to itself
'symbol ; a symbol atom; evaluates to itself when quoted
#t ; boolean true
(+ 1 2 3) ; an s-expression (function application)
'(4 'foo #t) ; quoted s-expression (a list)
;;; Comments
;;; Single-line comments start with a semicolon:
; This is a single-line comment
;;; Block comments use `#|` and `|#`:
#| This is a block comment.
It spans multiple lines.
|#
;;; REPL and environment
;;; Scheme is typically developed interactively in a Read-Eval-Print Loop (REPL).
;;; Implementations such as Racket, Guile, or MIT Scheme provide REPLs for interactive exploration.
;;; Libraries and tools can be installed depending on the specific implementation.
;;;-----------------------------------------------------------------------------
;;; 1. Primitive datatypes and operators
;;;-----------------------------------------------------------------------------
;;; Numbers
42 ; integers
#b101 ; binary => 5
#o777 ; octal => 511
#xFF ; hexadecimal => 255
3.14 ; floating-point numbers
1/2 ; fractions (exact rational numbers)
(make-rectangular 1 2) ; complex numbers
;;; Basic arithmetic
(+ 1 2) ; => 3
(- 7 3) ; => 4
(* 2 5) ; => 10
(/ 10 3) ; => 10/3
(sqrt 4) ; => 2
(expt 2 3) ; => 8
;;; Booleans
#t ; true
#f ; false
(and #t #f) ; => #f
(or #t #f) ; => #t
(not #t) ; => #f
;;; Strings
"Hello, World!"
(string-append "Hello, " "World!") ; => "Hello, World!"
;;; Lists
'(1 2 3) ; a list
(cons 1 '(2 3)) ; => '(1 2 3)
(car '(1 2 3)) ; => 1
(cdr '(1 2 3)) ; => '(2 3)
(append '(1 2) '(3 4)) ; => '(1 2 3 4)
;;;-----------------------------------------------------------------------------
;;; 2. Variables
;;;-----------------------------------------------------------------------------
;;; Define a variable
(define x 10)
x ; => 10
;;; Define a local variable
(let ((x 5)) (+ x 10)) ; => 15
x ; => 10 (unchanged globally)
;;;-----------------------------------------------------------------------------
;;; 3. Functions
;;;-----------------------------------------------------------------------------
;;; Define a named function
(define (square x)
(* x x))
(square 4) ; => 16
;;; Define an anonymous (lambda) function
((lambda (x) (* x x)) 5) ; => 25
;;; Higher-order functions
(define (apply-twice f x)
(f (f x)))
(apply-twice square 2) ; => 16
;;;-----------------------------------------------------------------------------
;;; 4. Conditionals and control flow
;;;-----------------------------------------------------------------------------
;;; If statements
(if (> 5 3)
'yes
'no) ; => 'yes
;;; Cond expressions (multi-branch conditionals)
(cond
[(< 5 3) 'less]
[(> 5 3) 'greater]
[else 'equal]) ; => 'greater
;;;-----------------------------------------------------------------------------
;;; 5. Structs and collections
;;;-----------------------------------------------------------------------------
;;; Define a structure
(define-struct dog (name breed age))
(define my-dog (make-dog "Fido" "Labrador" 5))
(dog-name my-dog) ; => "Fido"
(dog-age my-dog) ; => 5
;;;-----------------------------------------------------------------------------
;;; 6. Common patterns
;;;-----------------------------------------------------------------------------
;;; Recursive functions
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
(factorial 5) ; => 120
;;;-----------------------------------------------------------------------------
;;; 7. Libraries and modules
;;;-----------------------------------------------------------------------------
;;; Importing libraries/modules depends on the implementation.
;;; For example, in Racket:
(require racket/math)
(sqrt 16) ; => 4
;;;-----------------------------------------------------------------------------
;;; 8. Macros
;;;-----------------------------------------------------------------------------
;;; Macros allow you to create new syntactic constructs.
(define-syntax when
(syntax-rules ()
[(when test body ...)
(if test
(begin body ...))]))
(when #t
(display "Condition is true!\n")) ; Output: Condition is true!
;;;-----------------------------------------------------------------------------
;;; 9. Input and Output (I/O)
;;;-----------------------------------------------------------------------------
;;; Printing to the console
(display "Hello, Scheme!") ; => prints "Hello, Scheme!"
(newline) ; => moves to the next line
;;; Reading input
(let ((user-input (read)))
(display "You entered: ")
(display user-input))
;;; File I/O
(define output-port (open-output-file "example.txt"))
(display "Writing to a file." output-port)
(close-output-port output-port)
(define input-port (open-input-file "example.txt"))
(let ((file-content (read input-port)))
(display file-content))
(close-input-port input-port)
;;;-----------------------------------------------------------------------------
;;; 10. Iteration
;;;-----------------------------------------------------------------------------
;;; Iterating with `do`
(do ((i 0 (+ i 1))) ; initialize i to 0, increment by 1
((>= i 5)) ; stop when i >= 5
(display i) ; print i
(newline))
;;; Using recursion for iteration
(define (countdown n)
(if (= n 0)
(display "Blastoff!\n")
(begin
(display n)
(newline)
(countdown (- n 1)))))
(countdown 5) ; Output: 5 4 3 2 1 Blastoff!
;;;-----------------------------------------------------------------------------
;;; 11. Error handling
;;;-----------------------------------------------------------------------------
;;; Using `guard` for error handling (Racket example)
(guard [e (displayln (format "Error: ~a" e))]
(/ 1 0)) ; Output: Error: division by zero
;;; Catching exceptions manually
(with-handlers ([exn:fail? (lambda (e) (displayln "Caught an error!"))])
(error "Something went wrong!")) ; Output: Caught an error!
;;;-----------------------------------------------------------------------------
;;; 12. Advanced concepts
;;;-----------------------------------------------------------------------------
;;; Continuations with `call/cc`
(call/cc
(lambda (cont)
(display "Before continuation\n")
(cont #f)
(display "After continuation\n"))) ; Output: Before continuation
;;; Lazy evaluation (streams)
(define (make-stream start step)
(cons start
(lambda () (make-stream (+ start step) step))))
(define nums (make-stream 0 1)) ; Infinite stream starting at 0, incrementing by 1
(define (stream-ref stream n)
(if (= n 0)
(car stream)
(stream-ref ((cdr stream)) (- n 1))))
(stream-ref nums 5) ; => 5
;;;-----------------------------------------------------------------------------
;;; 13. Meta-programming
;;;-----------------------------------------------------------------------------
;;; Evaluate expressions dynamically
(eval '(+ 1 2)) ; => 3
;;; Quasiquoting for meta-programming
`(1 2 ,(+ 3 4)) ; => '(1 2 7)
```