This commit is contained in:
NaiJi ✨ 2024-04-17 03:13:00 +04:00
commit 6f7b5bf196
63 changed files with 865 additions and 0 deletions

3
A Tour of Nix/01.nix Normal file
View File

@ -0,0 +1,3 @@
{
helloWorld = "Hello" + " " + "World";
}

4
A Tour of Nix/02.nix Normal file
View File

@ -0,0 +1,4 @@
# code goes here
{
v="understood";
}

8
A Tour of Nix/03.nix Normal file
View File

@ -0,0 +1,8 @@
let
h = "Hello";
w = "World";
s = " ";
in
{
helloWorld = h + s + w;
}

6
A Tour of Nix/04.nix Normal file
View File

@ -0,0 +1,6 @@
let
h = "Hello";
in
{
helloWorld = "${h} World"; # Modify this line
}

7
A Tour of Nix/05.nix Normal file
View File

@ -0,0 +1,7 @@
let
h = "Strings";
value = 4;
in
{
helloWorld = "${h} ${toString value} the win!";
}

8
A Tour of Nix/06.nix Normal file
View File

@ -0,0 +1,8 @@
let
f = "f";
o = "o";
func = a: b: c: a + b + c;
in
{
foo = func f o "o";
}

8
A Tour of Nix/07.nix Normal file
View File

@ -0,0 +1,8 @@
let
f = "f";
o = "o";
func = {a, b, c}: a + b + c;
in
{
foo = func {a=f; b=o; c="o";};
}

8
A Tour of Nix/08.nix Normal file
View File

@ -0,0 +1,8 @@
let
min = a: b: if (a > b) then b else a; #modify these
max = a: b: if (a > b) then a else b; #two lines only
in
{
ex0 = min 5 3;
ex1 = max 9 4;
}

11
A Tour of Nix/09.nix Normal file
View File

@ -0,0 +1,11 @@
let
f = "f";
o = "o";
b = "b";
func = {a ? f, b ? "a", c ? ""}: a+b+c; #only modify this line!
in
rec {
foo = func {b="o"; c=o;}; #must evaluate to "foo"
bar = func {a=b; c="r";}; #must evaluate to "bar"
foobar = func {a=foo;b=bar;}; #must evaluate to "foobar"
}

11
A Tour of Nix/10.nix Normal file
View File

@ -0,0 +1,11 @@
let
arguments = {a="f"; b="o"; c="o"; d="bar";}; #only modify this line
func = {a, b, c, ...}: a+b+c;
func2 = args@{a, b, c, ...}: a+b+c+args.d;
in
{
#the argument d is not used
foo = func arguments;
#now the argument d is used
foobar = func2 arguments;
}

8
A Tour of Nix/11.nix Normal file
View File

@ -0,0 +1,8 @@
let
func = {a, b, ...}@bargs: if a == "foo" then
b + bargs.c else b + bargs.x + bargs.y;
in
{
#complete next line so it evaluates to "foobar"
foobar = func {a="bar"; b="foo"; x="bar"; y="";}; #ONLY EDIT THIS LINE
}

14
A Tour of Nix/12.nix Normal file
View File

@ -0,0 +1,14 @@
let
b = 1;
fu0 = (x: x);
fu1 = (x: y: x + y) 4;
fu2 = (x: y: (2 * x) + y);
in
rec {
ex00 = fu0 4; # must return 4
ex01 = (fu1) 1; # must return 5
ex02 = (fu2 3 ) 1; # must return 7
ex03 = (fu2 3 ); # must return <LAMBDA>
ex04 = ex03 1; # must return 7
ex05 = (n: x: (fu2 x n)) 1 3; # must return 7
}

8
A Tour of Nix/13.nix Normal file
View File

@ -0,0 +1,8 @@
let
arguments = {a="Happy"; b="Awesome";};
func = {a, b}: {d, b, c}: a+b+c+d;
in
{
A = func arguments {c="Are"; b="Functions"; d="Called";}; #only modify this line
}

4
A Tour of Nix/14.nix Normal file
View File

@ -0,0 +1,4 @@
rec {
x = "a";
y = x;
}

18
A Tour of Nix/15.nix Normal file
View File

@ -0,0 +1,18 @@
with import <nixpkgs> { };
with stdenv.lib;
let
attr = {a="a"; b = 1; c = true;};
s = "b";
in
{
#replace all X, everything should evaluate to true
ex0 = isAttrs attr;
ex1 = attr.a == "a";
ex2 = attr.${s} == 1;
ex3 = attrVals ["c" "b"] attr == [ true 1 ];
ex4 = attrValues attr == [ "a" 1 true ];
ex5 = builtins.intersectAttrs attr {a="b"; d=234; c="";}
== { a = "b"; c = "";};
ex6 = removeAttrs attr ["b" "c"] == { a="a"; };
ex7 = ! attr ? a == false;
}

9
A Tour of Nix/16.nix Normal file
View File

@ -0,0 +1,9 @@
let
list = [ { name = "foo"; value = 123; }
{ name = "bar"; value = 456; } ];
string = ''{"x": [1, 2, 3], "y": null}'';
in
{
ex0 = builtins.listToAttrs list == { foo = 123; bar = 456; };
ex1 = builtins.fromJSON string == { x = [ 1 2 3 ]; y = null; };
}

9
A Tour of Nix/17.nix Normal file
View File

@ -0,0 +1,9 @@
let
attrSetBonus = {f = {add = (x: y: x + y);
mul = (x: y: x * y);};
n = {one = 1; two = 2;};};
in
rec {
#Bonus: use only the attrSetBonus to solve this one
exBonus = 5 == attrSetBonus.f.add attrSetBonus.n.one ( attrSetBonus.f.add attrSetBonus.n.two attrSetBonus.n.two);
}

15
A Tour of Nix/18.nix Normal file
View File

@ -0,0 +1,15 @@
let
x = { a="bananas"; b= "pineapples"; };
y = { a="kakis"; c ="grapes";};
z = { a="raspberrys"; c= "oranges"; };
func = {a, b, c ? "another secret ingredient"}: "A drink of: " +
a + ", " + b + " and " + c;
in
rec {
ex00=func ( x );
# hit 'run', you need the output to solve this!
ex01=func (y // x);
ex02=func (x // { c="lychees";});
ex03=func (z // x // z);
}

21
A Tour of Nix/19.nix Normal file
View File

@ -0,0 +1,21 @@
let
attrSet = {x = "a"; y = "b"; b = {t = true; f = false;};};
attrSet.c = 1;
attrSet.d = null;
attrSet.e.f = "g";
in
rec {
#boolean
ex0 = attrSet.b.t;
#equal
ex01 = "a" == attrSet.x;
#unequal
ex02 = !("b" != attrSet.y );
#and/or/neg
ex03 = ex01 && !ex02 || ! attrSet.b.f;
#implication
ex04 = true -> attrSet.b.t;
#contains attribute
ex05 = attrSet ? x;
ex06 = attrSet.b ? f;
}

23
A Tour of Nix/20.nix Normal file
View File

@ -0,0 +1,23 @@
with import <nixpkgs> { };
with stdenv.lib;
let
list = [2 "4" true true {a = 27;} 2];
f = x: isString x;
s = "foobar";
in
{
#replace all X, everything should evaluate to true
ex00 = isList list;
ex01 = elemAt list 2 == true;
ex02 = length list == 6;
ex03 = last list == 2;
ex04 = filter f list == [ "4" ];
ex05 = head list == 2;
ex06 = tail list == [ "4" true true {a = 27;} 2 ];
ex07 = remove true list == [ 2 "4" {a = 27;} 2 ];
ex08 = toList s == [ "foobar" ];
ex09 = take 3 list == [ 2 "4" true ];
ex10 = drop 4 list == [ {a = 27;} 2 ];
ex11 = unique list == [ 2 "4" true {a = 27;} ];
ex12 = list ++ ["x" "y"] == [ 2 "4" true true {a = 27;} 2 "x" "y" ];
}

14
A Tour of Nix/21.nix Normal file
View File

@ -0,0 +1,14 @@
let
attrSet = {x = "a"; y = "b"; b = {t = true; f = false;};};
attrSet.c = 1;
attrSet.d = 2;
attrSet.e.f = "g";
list1 = [attrSet.c attrSet.d];
list2 = [attrSet.x attrSet.y];
in
{
#List concatenation.
ex0 = ["a" "b" 1 2] == [ attrSet.x attrSet.y ] ++ [attrSet.c attrSet.d ];
}

12
A Tour of Nix/22.nix Normal file
View File

@ -0,0 +1,12 @@
let
myImport = import <nixpkgs> {};
x = 123;
as = { a = "foo"; b = "bar"; };
in with as; {
inherit x; #example
#fix line below: we want a and b in this scope
inherit a b;
#also fix this line
z = myImport.lib.isBool true;
}

9
A Tour of Nix/23.nix Normal file
View File

@ -0,0 +1,9 @@
let
x = 123;
as = { a = "foo"; b = "bar"; x="234"; };
in with as; {
res = x; # what value is res bound to?
}
# naiji: 'let' has priority over 'with'

16
A Tour of Nix/24.nix Normal file
View File

@ -0,0 +1,16 @@
with import <nixpkgs> {};
with lib;
{
ex00 = isAttrs {};
ex01 = isAttrs {};
ex02 = isString "a";
ex03 = isInt (-3);
ex04 = isFunction (x: x);
ex05 = isString (x:x);
ex06 = isString ("x");
ex07 = isNull null;
ex08 = isFunction (y: y+1);
ex09 = isList [({z}: z) (x: x)];
ex10 = isAttrs {a=[];};
ex11 = isInt (-10); # oh, what is that?
}

11
A Tour of Nix/25.nix Normal file
View File

@ -0,0 +1,11 @@
with import <nixpkgs> {};
let
func = x: y: assert (x==2) || abort "x has to be 2 or it won't work!"; x + y;
n = -1; # only modify this line
in
assert (lib.isInt n) || abort "Type error since supplied argument is no int!";
rec {
ex00 = func (n+3) 3;
}

26
A Tour of Nix/26.nix Normal file
View File

@ -0,0 +1,26 @@
let
#dummyfunctions
fetchurl = x: x;
ncurses = "ncurses";
gettext = "gettext";
in
rec {
pname = "nano";
version = "2.3.6";
name = "${pname}-${version}";
src = fetchurl {
url = "mirror://gnu/nano/${name}.tar.gz";
sha256 = "a74bf3f18b12c1c777ae737c0e463152439e381aba8720b4bc67449f36a09534";
};
buildInputs = [ ncurses gettext ];
configureFlags = "sysconfdir=/etc";
meta = {
homepage = http://www.nano-editor.org/;
description = "A small, user-friendly console text editor";
};
}

10
A Tour of Nix/27.nix Normal file
View File

@ -0,0 +1,10 @@
let
bar = ["bar" "foo" "bla"];
numbers = [1 2 3 4];
in
{
#multiplies every number by 2
example = map (n: n * 2) numbers;
#complete this
foobar = map ( bar: bar + "bar" ) bar;
}

11
A Tour of Nix/28.nix Normal file
View File

@ -0,0 +1,11 @@
with import <nixpkgs> { };
let
list = ["a" "b" "a" "c" "d" "a"];
countA = lib.fold (x: y: if x != "a" then y else y + 1) 0;
in
rec {
example = lib.fold (x: y: x + y) "" ["a" "b" "c"]; #is "abc"
result = countA list; #should be 3
}
# fold f z [x_1 x_2 ... x_n] == f x_1 (f x_2 ... (f x_n z))

9
A Tour of Nix/29.nix Normal file
View File

@ -0,0 +1,9 @@
with import <nixpkgs> { };
let
listOfNumbers = [2 4 6 9 27];
reverseList = lib.fold (x: y: y ++ [x]) [];
in
rec {
example = lib.reverseList listOfNumbers;
result = reverseList listOfNumbers;
}

10
A Tour of Nix/30.nix Normal file
View File

@ -0,0 +1,10 @@
with import <nixpkgs> { };
let
listOfNumbers = [2 4 6 9 27];
myMap = f: l: lib.fold (x: y: [(f x)] ++ y) [] l;
in
rec {
#your map should create the same result as the standard map function
example = map (x: builtins.div x 2) listOfNumbers;
result = myMap (x: builtins.div x 2) listOfNumbers;
}

15
A Tour of Nix/31.nix Normal file
View File

@ -0,0 +1,15 @@
with import <nixpkgs> { };
let
attrSet = {c = 3; a = 1; b = 2;};
#This is an example function that extracts a single value
getSingleVal = (attrSet: x: attrSet.${x});
#tips: use the map function and access the attribute values
#in the same way as 'getSingleVal'
attrVals = l: s: map (field: s.${field}) l;
in
rec {
example = getSingleVal attrSet "a"; #is [1]
solution = attrVals ["a" "b" "c"] attrSet; #should be [1 2 3]
}

9
A Tour of Nix/32.nix Normal file
View File

@ -0,0 +1,9 @@
with import <nixpkgs> { };
let
attrSet = {c = 3; a = 1; b = 2;};
attrValues = s: map (field: s.${field}) (builtins.attrNames s);
in
rec {
solution = attrValues attrSet; #should be [1 2 3]
}

11
A Tour of Nix/33.nix Normal file
View File

@ -0,0 +1,11 @@
with import <nixpkgs> { };
let
list = [["a"] ["b"] ["c"]];
attrList = [{a = 1;} {b = 0;} {a = 2;}];
catAttrs = atr: l: lib.fold (val: _l: if (builtins.hasAttr atr val) then [ (val.${atr}) ] ++ _l else _l) [] l;
in
rec {
example = builtins.concatLists list; #is [ "a" "b" "c" ]
result = catAttrs "a" attrList; #should be [1 2]
}

18
A Tour of Nix/34.nix Normal file
View File

@ -0,0 +1,18 @@
with import <nixpkgs> { };
rec {
made_it = "it is done";
}
#Further reading
#Nix by example - Part 1 : https://medium.com/@MrJamesFisher/nix-by-example-a0063a1a4c55
#Parse trees, evaluation order, composite data-types, laziness, conditionals, Let expressions, and much more...
#Luca Bruno's nix pill(s) : http://lethalman.blogspot.de/2014/07/nix-pill-1-why-you-should-give-it-try.html
#The Nix Pills are a wonderful introduction into Nix programming and you will have much joy reading them!
#NixPkgs manual : https://nixos.org/nixpkgs/manual
#Covers topics as: buildPhases, override(s) and support for specific programming languages
#NixOS Wiki : https://nixos.org/wiki/Main_Page
#The Wiki contains a lot of practical articles, like the Cheatsheet

3
A Tour of Nix/README.md Normal file
View File

@ -0,0 +1,3 @@
# A Tour of Nix
My solutions for online excercises [A Tour of Nix](https://nixcloud.io/tour)

View File

@ -0,0 +1 @@
*.bak

View File

@ -0,0 +1,4 @@
# sicp-exs
Trying out exercies from "Structure and Interpretation of Computer Programs" second edition
[Structure and Interpretation of Computer Programs (SICP)](https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs)

View File

@ -0,0 +1,27 @@
; f(n) = n, when n < 3
; f(n) = f(n - 1) + f(n - 2) + f(n - 3), when n >= 3
(define fn 15)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; recursive
(define (f-rec n)
(if (< n 3)
n
(+ (+ (f-rec (- n 1))
(f-rec (- n 2)))
(f-rec (- n 3)))))
(f-rec fn)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; iterative
(define (f-iter n)
(f-iter-step 2 1 0 n))
(define (f-iter-step a b c count)
(if (< count 3)
a
(f-iter-step (+ a b c) a b (- count 1))))
(f-iter fn)

View File

@ -0,0 +1,33 @@
; 1
; 1 1
; 1 2 1
; 1 3 3 1
; 1 4 6 4 1
; . . . .
; I am sure that I have chosen the worst possible way
(define (pasc-step row col num curr-row curr-col)
(cond
((or (< col 0) (> col row)) 0)
((or (= col 1) (= col row)) 1)
((= curr-row row) num)
((or (= col 2) (= col (- row 1))) (- row 1))
((or (> col 2) (< col (- row 1))) (pasc-step
row
col
(+ num (if (< curr-col col)
(pasc-step curr-row (+ curr-col 1) 1 2 2)
(pasc-step curr-row (- curr-col 1) 1 2 2)))
(+ curr-row 1)
(if (< curr-col col) (+ curr-col 1) curr-col)))))
(define (locate-pasc row col)
(pasc-step row col 1 2 2))
(locate-pasc 2 1)
(locate-pasc 3 2)
(locate-pasc 4 3)
(locate-pasc 5 2)
(locate-pasc 5 4)
(locate-pasc 6 4)
(locate-pasc 7 3)

View File

@ -0,0 +1,19 @@
#! /usr/bin/racket
#lang racket/base
(define (sqr n)
(* n n))
(define (fast-ext-iter number power product)
(cond ((= power 0) product)
((even? power) (fast-ext-iter (sqr number)
(/ power 2)
product))
(else (fast-ext-iter number
(- power 1)
(* number product)))))
(define (fast-ext number power)
(fast-ext-iter number power 1))
(fast-ext 2 5)

View File

@ -0,0 +1,43 @@
(define (multiply a b)
(define (iter a b product)
(if (= b 0)
product
(iter a (- b 1) (+ product a))))
(iter a b 0))
; multiply
(define (sqr n)
(multiply n n))
(define (divide a b)
(define (iter a b product)
(cond ((= a 0) product)
((< a 0) (- product 1))
(else (iter (- a b) b (+ product 1)))))
(iter a b 0))
; divide
(define (halve a)
(divide a 2))
(define (fast-ext number power)
(define (iter number power product)
(cond ((= power 0) product)
((even? power) (iter (sqr number)
(halve power)
product))
(else (iter number
(- power 1)
(multiply number product)))))
(iter number power 1))
; fast-ext
(fast-ext 5 3)
(fast-ext 2 2)
(fast-ext 1 2)
(fast-ext 9 4)
(fast-ext 2 0)
(fast-ext 7 1)

View File

@ -0,0 +1,28 @@
(define (divide a b)
(define (iter a b product)
(cond ((= a 0) product)
((< a 0) (- product 1))
(else (iter (- a b) b (+ product 1)))))
(iter a b 0))
; divide
(define (halve a)
(divide a 2))
(define (double a)
(+ a a))
(define (multiply a b)
(define (iter a b product)
(cond ((= a 1) (+ product b))
((even? a) (iter (halve a) (double b) product))
(else (iter (halve a) (double b) (+ product b)))))
(iter a b 0))
; multiply
(multiply 132 555)
(multiply 32 2)
(multiply 33 1)

View File

@ -0,0 +1,7 @@
(define the-numerator
(+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))))
(define the-denominator
(* 3 (- 6 2)(- 2 7)))
(/ the-numerator the-denominator)

View File

@ -0,0 +1,17 @@
(define (square n)
(* n n))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b)
(= (remainder b a) 0))
(smallest-divisor 199)
(smallest-divisor 1999)
(smallest-divisor 19999)

View File

@ -0,0 +1,21 @@
(define (square n)
(* n n))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(define (next iterator)
(if (= iterator 2) 3 (+ iterator 2)))
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
; find-divisor
(define (divides? a b)
(= (remainder b a) 0))
(smallest-divisor 199)
(smallest-divisor 1999)
(smallest-divisor 19999)

View File

@ -0,0 +1,36 @@
#lang racket ; needed for true / false
(define (square n)
(* n n))
(define (expmod base exp m)
(cond ((= exp 0) 1)
((even? exp)
(remainder (square (expmod base (/ exp 2) m))
m))
(else
(remainder (* base (expmod base (- exp 1) m))
m))))
(define (fermat-test n smaller-n)
(define (try-it a)
(= (expmod a n n) a))
(cond ((= smaller-n 1) true)
((try-it smaller-n) (fermat-test n (- smaller-n 1)))
(else false)))
(define (is-carmichael-prime? n)
(fermat-test n (- n 1)))
561
(is-carmichael-prime? 561)
1105
(is-carmichael-prime? 1105)
1729
(is-carmichael-prime? 1729)
2465
(is-carmichael-prime? 2465)
2821
(is-carmichael-prime? 2821)
6601
(is-carmichael-prime? 6601)

View File

@ -0,0 +1,11 @@
(define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))
(define (is-first-least? a b c) (and (< a b) (< a c)))
(define a 9)
(define b 1)
(define c 10)
(cond ((is-first-least? c a b)(sum-of-squares a b))
((is-first-least? a c b)(sum-of-squares c b))
((is-first-least? b a c)(sum-of-squares a c)))

View File

@ -0,0 +1,11 @@
#! /usr/bin/racket
#lang racket/base
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ (term a) result))))
(iter a 0))
(sum (lambda (x) x) 0 (lambda (x) (+ x 1)) 3)

View File

@ -0,0 +1,27 @@
#! /usr/bin/racket
#lang racket/base
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* (term a) result))))
(iter a 1))
(define (numerator pre-applying n)
(* pre-applying (product (lambda (x) (* x x)) 4 (lambda (x) (+ x 2)) n)))
(define (denominator pre-applying n)
(* pre-applying (product (lambda (x) (* x x)) 3 (lambda (x) (+ x 2)) n)))
(define (fourth-of-pi n)
(cond ((< n 1) 0)
((even? n) (/ (numerator (* 2 (+ n 2)) n)
(denominator 1 ( + n 1))))
(else (/ (numerator 2 (+ n 1))
(denominator (+ n 2) n)))))
(fourth-of-pi 1)
(fourth-of-pi 2)
(fourth-of-pi 3)
(fourth-of-pi 60)

View File

@ -0,0 +1,35 @@
#! /usr/bin/racket
#lang racket/base
(define (accumulate-iter combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner (term a) result))))
(iter a null-value))
(define (product-iter term a next b)
(accumulate-iter * 1 term a next b))
(define (sum-iter term a next b)
(accumulate-iter + 0 term a next b))
(sum-iter (lambda (x) x) 1 (lambda (x) (+ x 1)) 4)
(product-iter (lambda (x) x) 1 (lambda (x) (+ x 1)) 4)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (accumulate-recur combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a)
(accumulate-recur combiner null-value term (next a) next b))))
(define (product-recur term a next b)
(accumulate-recur * 1 term a next b))
(define (sum-recur term a next b)
(accumulate-recur + 0 term a next b))
(sum-recur (lambda (x) x) 1 (lambda (x) (+ x 1)) 4)
(product-recur (lambda (x) x) 1 (lambda (x) (+ x 1)) 4)

View File

@ -0,0 +1,33 @@
#! /usr/bin/racket
#lang racket/base
(define (square n)
(* n n))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(define (next iterator)
(if (= iterator 2) 3 (+ iterator 2)))
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (divides? a b)
(= (remainder b a) 0))
(define (prime? n)
(= n (smallest-divisor n)))
(define (filtered-accumulate combiner null-value predicate term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner (if (predicate a) (term a) null-value) result))))
(iter a null-value))
(define (range-sum-of-primes a b)
(filtered-accumulate + 0 prime? (lambda (x) x) a (lambda (x) (+ 1 x)) b))
(range-sum-of-primes 2 10)

View File

@ -0,0 +1,23 @@
(define (sqrt-iter guess guess-prev x)
(if (good-enough? guess guess-prev)
guess
(sqrt-iter (improve guess x) guess
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess guess-prev)
(< (abs (- guess guess-prev)) 0.001))
(define (sqrt-new x)
(if (= x 0) x (sqrt-iter 1.0 999 x)))
(sqrt-new 16)
(sqrt-new 1.0004)
(sqrt-new 1)
(sqrt-new 134895724398)
(sqrt-new 0)

View File

@ -0,0 +1,23 @@
(define (square x) (* x x))
(define (cubic-iter guess guess-prev x)
(if (good-enough? guess guess-prev)
guess
(cubic-iter (improve guess x) guess
x)))
(define (improve guess x)
(average (* 2 guess) (/ x (square guess))))
(define (average x y)
(/ (+ x y) 3))
(define (good-enough? guess guess-prev)
(< (abs (- guess guess-prev)) 0.001))
(define (cubic-root x)
(if (= x 0) x (cubic-iter 1.0 999 x)))
(cubic-root 1892379832.0)
(cubic-root 8.0)
(cubic-root 27.0)

View File

@ -0,0 +1,8 @@
#include <stdio.h>
main()
{
printf("hello,");
printf(" world");
printf("\n");
}

View File

@ -0,0 +1,7 @@
#include <stdio.h>
main()
{
// "Unknown escape sequence"
printf("i am breaking this\j\n");
}

View File

@ -0,0 +1,20 @@
#include <stdio.h>
main()
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
printf("%10s %7s\n", "Fahrenheit", "Celsius");
while (fahr <= upper)
{
celsius = (5.0 / 9.0) * (fahr - 32.0);
printf("%10.0f %7.1f\n", fahr, celsius);
fahr = fahr + step;
}
}

View File

@ -0,0 +1,20 @@
#include <stdio.h>
main()
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
celsius = lower;
printf("%10s %7s\n", "Fahrenheit", "Celsius");
while (celsius <= upper)
{
fahr = (celsius * 9.0 / 5.0) + 32.0;
printf("%10.0f %7.1f\n", fahr, celsius);
celsius = celsius + step;
}
}

View File

@ -0,0 +1,9 @@
#include <stdio.h>
main()
{
for (int fahr = 300; fahr >= 0; fahr = fahr - 20)
{
printf("%3d %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));
}
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
#include <stdio.h>
main()
{
printf("EOFing is 1: %d\n", getchar() != EOF);
}

View File

@ -0,0 +1,6 @@
#include <stdio.h>
main()
{
printf("EOF: %d\n", EOF);
}

View File

@ -0,0 +1,3 @@
# The C Programming Language
Here are my solutions and snippets from [The C Programming Language](https://en.wikipedia.org/wiki/The_C_Programming_Language).

BIN
The C Programming Language/a.out Executable file

Binary file not shown.