commit 6f7b5bf196ae14a8bc39c41275c6f22e0fbbfb9f Author: NaiJi Date: Wed Apr 17 03:13:00 2024 +0400 init diff --git a/A Tour of Nix/01.nix b/A Tour of Nix/01.nix new file mode 100644 index 0000000..e1285f5 --- /dev/null +++ b/A Tour of Nix/01.nix @@ -0,0 +1,3 @@ +{ + helloWorld = "Hello" + " " + "World"; +} diff --git a/A Tour of Nix/02.nix b/A Tour of Nix/02.nix new file mode 100644 index 0000000..8df375b --- /dev/null +++ b/A Tour of Nix/02.nix @@ -0,0 +1,4 @@ +# code goes here +{ + v="understood"; +} diff --git a/A Tour of Nix/03.nix b/A Tour of Nix/03.nix new file mode 100644 index 0000000..30f4ac8 --- /dev/null +++ b/A Tour of Nix/03.nix @@ -0,0 +1,8 @@ +let + h = "Hello"; + w = "World"; + s = " "; +in +{ + helloWorld = h + s + w; +} diff --git a/A Tour of Nix/04.nix b/A Tour of Nix/04.nix new file mode 100644 index 0000000..b090534 --- /dev/null +++ b/A Tour of Nix/04.nix @@ -0,0 +1,6 @@ +let + h = "Hello"; +in +{ + helloWorld = "${h} World"; # Modify this line +} diff --git a/A Tour of Nix/05.nix b/A Tour of Nix/05.nix new file mode 100644 index 0000000..fb176a6 --- /dev/null +++ b/A Tour of Nix/05.nix @@ -0,0 +1,7 @@ +let + h = "Strings"; + value = 4; +in +{ + helloWorld = "${h} ${toString value} the win!"; +} diff --git a/A Tour of Nix/06.nix b/A Tour of Nix/06.nix new file mode 100644 index 0000000..8c96dec --- /dev/null +++ b/A Tour of Nix/06.nix @@ -0,0 +1,8 @@ +let + f = "f"; + o = "o"; + func = a: b: c: a + b + c; +in +{ + foo = func f o "o"; +} diff --git a/A Tour of Nix/07.nix b/A Tour of Nix/07.nix new file mode 100644 index 0000000..042143e --- /dev/null +++ b/A Tour of Nix/07.nix @@ -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";}; +} diff --git a/A Tour of Nix/08.nix b/A Tour of Nix/08.nix new file mode 100644 index 0000000..b0dba5a --- /dev/null +++ b/A Tour of Nix/08.nix @@ -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; +} diff --git a/A Tour of Nix/09.nix b/A Tour of Nix/09.nix new file mode 100644 index 0000000..c3a49dd --- /dev/null +++ b/A Tour of Nix/09.nix @@ -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" +} diff --git a/A Tour of Nix/10.nix b/A Tour of Nix/10.nix new file mode 100644 index 0000000..39426d2 --- /dev/null +++ b/A Tour of Nix/10.nix @@ -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; +} diff --git a/A Tour of Nix/11.nix b/A Tour of Nix/11.nix new file mode 100644 index 0000000..1f734b9 --- /dev/null +++ b/A Tour of Nix/11.nix @@ -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 +} \ No newline at end of file diff --git a/A Tour of Nix/12.nix b/A Tour of Nix/12.nix new file mode 100644 index 0000000..8848e97 --- /dev/null +++ b/A Tour of Nix/12.nix @@ -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 + ex04 = ex03 1; # must return 7 + ex05 = (n: x: (fu2 x n)) 1 3; # must return 7 +} \ No newline at end of file diff --git a/A Tour of Nix/13.nix b/A Tour of Nix/13.nix new file mode 100644 index 0000000..7889d0d --- /dev/null +++ b/A Tour of Nix/13.nix @@ -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 +} \ No newline at end of file diff --git a/A Tour of Nix/14.nix b/A Tour of Nix/14.nix new file mode 100644 index 0000000..ef51723 --- /dev/null +++ b/A Tour of Nix/14.nix @@ -0,0 +1,4 @@ +rec { + x = "a"; + y = x; +} \ No newline at end of file diff --git a/A Tour of Nix/15.nix b/A Tour of Nix/15.nix new file mode 100644 index 0000000..3db6c09 --- /dev/null +++ b/A Tour of Nix/15.nix @@ -0,0 +1,18 @@ +with import { }; +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; +} \ No newline at end of file diff --git a/A Tour of Nix/16.nix b/A Tour of Nix/16.nix new file mode 100644 index 0000000..74fafd7 --- /dev/null +++ b/A Tour of Nix/16.nix @@ -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; }; +} \ No newline at end of file diff --git a/A Tour of Nix/17.nix b/A Tour of Nix/17.nix new file mode 100644 index 0000000..7db57ea --- /dev/null +++ b/A Tour of Nix/17.nix @@ -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); +} \ No newline at end of file diff --git a/A Tour of Nix/18.nix b/A Tour of Nix/18.nix new file mode 100644 index 0000000..5d7017c --- /dev/null +++ b/A Tour of Nix/18.nix @@ -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); +} \ No newline at end of file diff --git a/A Tour of Nix/19.nix b/A Tour of Nix/19.nix new file mode 100644 index 0000000..c889db8 --- /dev/null +++ b/A Tour of Nix/19.nix @@ -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; +} \ No newline at end of file diff --git a/A Tour of Nix/20.nix b/A Tour of Nix/20.nix new file mode 100644 index 0000000..599e961 --- /dev/null +++ b/A Tour of Nix/20.nix @@ -0,0 +1,23 @@ +with import { }; +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" ]; +} diff --git a/A Tour of Nix/21.nix b/A Tour of Nix/21.nix new file mode 100644 index 0000000..c8c3382 --- /dev/null +++ b/A Tour of Nix/21.nix @@ -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 ]; +} \ No newline at end of file diff --git a/A Tour of Nix/22.nix b/A Tour of Nix/22.nix new file mode 100644 index 0000000..c6dd310 --- /dev/null +++ b/A Tour of Nix/22.nix @@ -0,0 +1,12 @@ +let + myImport = import {}; + 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; +} \ No newline at end of file diff --git a/A Tour of Nix/23.nix b/A Tour of Nix/23.nix new file mode 100644 index 0000000..b63fc21 --- /dev/null +++ b/A Tour of Nix/23.nix @@ -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' \ No newline at end of file diff --git a/A Tour of Nix/24.nix b/A Tour of Nix/24.nix new file mode 100644 index 0000000..438315d --- /dev/null +++ b/A Tour of Nix/24.nix @@ -0,0 +1,16 @@ +with import {}; +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? +} \ No newline at end of file diff --git a/A Tour of Nix/25.nix b/A Tour of Nix/25.nix new file mode 100644 index 0000000..8342686 --- /dev/null +++ b/A Tour of Nix/25.nix @@ -0,0 +1,11 @@ +with import {}; +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; +} \ No newline at end of file diff --git a/A Tour of Nix/26.nix b/A Tour of Nix/26.nix new file mode 100644 index 0000000..ad3143a --- /dev/null +++ b/A Tour of Nix/26.nix @@ -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"; + }; +} diff --git a/A Tour of Nix/27.nix b/A Tour of Nix/27.nix new file mode 100644 index 0000000..9499b8e --- /dev/null +++ b/A Tour of Nix/27.nix @@ -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; +} diff --git a/A Tour of Nix/28.nix b/A Tour of Nix/28.nix new file mode 100644 index 0000000..8c9a2f1 --- /dev/null +++ b/A Tour of Nix/28.nix @@ -0,0 +1,11 @@ +with import { }; +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)) \ No newline at end of file diff --git a/A Tour of Nix/29.nix b/A Tour of Nix/29.nix new file mode 100644 index 0000000..5d42620 --- /dev/null +++ b/A Tour of Nix/29.nix @@ -0,0 +1,9 @@ +with import { }; +let + listOfNumbers = [2 4 6 9 27]; + reverseList = lib.fold (x: y: y ++ [x]) []; +in +rec { + example = lib.reverseList listOfNumbers; + result = reverseList listOfNumbers; +} \ No newline at end of file diff --git a/A Tour of Nix/30.nix b/A Tour of Nix/30.nix new file mode 100644 index 0000000..7816178 --- /dev/null +++ b/A Tour of Nix/30.nix @@ -0,0 +1,10 @@ +with import { }; +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; +} \ No newline at end of file diff --git a/A Tour of Nix/31.nix b/A Tour of Nix/31.nix new file mode 100644 index 0000000..fdc4aa0 --- /dev/null +++ b/A Tour of Nix/31.nix @@ -0,0 +1,15 @@ +with import { }; +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] +} \ No newline at end of file diff --git a/A Tour of Nix/32.nix b/A Tour of Nix/32.nix new file mode 100644 index 0000000..c764e2a --- /dev/null +++ b/A Tour of Nix/32.nix @@ -0,0 +1,9 @@ +with import { }; +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] +} \ No newline at end of file diff --git a/A Tour of Nix/33.nix b/A Tour of Nix/33.nix new file mode 100644 index 0000000..9b9a752 --- /dev/null +++ b/A Tour of Nix/33.nix @@ -0,0 +1,11 @@ +with import { }; +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] +} \ No newline at end of file diff --git a/A Tour of Nix/34.nix b/A Tour of Nix/34.nix new file mode 100644 index 0000000..4f06bcd --- /dev/null +++ b/A Tour of Nix/34.nix @@ -0,0 +1,18 @@ +with import { }; +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 \ No newline at end of file diff --git a/A Tour of Nix/README.md b/A Tour of Nix/README.md new file mode 100644 index 0000000..0e30fd9 --- /dev/null +++ b/A Tour of Nix/README.md @@ -0,0 +1,3 @@ +# A Tour of Nix + +My solutions for online excercises [A Tour of Nix](https://nixcloud.io/tour) diff --git a/Structure and Interpretation of Computer Programs/.gitignore b/Structure and Interpretation of Computer Programs/.gitignore new file mode 100644 index 0000000..751553b --- /dev/null +++ b/Structure and Interpretation of Computer Programs/.gitignore @@ -0,0 +1 @@ +*.bak diff --git a/Structure and Interpretation of Computer Programs/README.md b/Structure and Interpretation of Computer Programs/README.md new file mode 100644 index 0000000..b0ab129 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/README.md @@ -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) diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.11.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.11.rkt new file mode 100755 index 0000000..2d858dd --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.11.rkt @@ -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) + diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.12.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.12.rkt new file mode 100755 index 0000000..004cbe1 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.12.rkt @@ -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) \ No newline at end of file diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.16.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.16.rkt new file mode 100755 index 0000000..419c02e --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.16.rkt @@ -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) \ No newline at end of file diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.17.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.17.rkt new file mode 100755 index 0000000..60ea611 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.17.rkt @@ -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) \ No newline at end of file diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.18.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.18.rkt new file mode 100755 index 0000000..80cac03 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.18.rkt @@ -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) diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.2.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.2.rkt new file mode 100755 index 0000000..9663a3e --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.2.rkt @@ -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) \ No newline at end of file diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.21.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.21.rkt new file mode 100755 index 0000000..18e5477 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.21.rkt @@ -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) diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.23.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.23.rkt new file mode 100755 index 0000000..3275568 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.23.rkt @@ -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) diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.27.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.27.rkt new file mode 100755 index 0000000..212a708 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.27.rkt @@ -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) \ No newline at end of file diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.3.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.3.rkt new file mode 100755 index 0000000..55c32e9 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.3.rkt @@ -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))) \ No newline at end of file diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.30.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.30.rkt new file mode 100755 index 0000000..a0d105b --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.30.rkt @@ -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) diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.31.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.31.rkt new file mode 100644 index 0000000..ebe2f90 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.31.rkt @@ -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) diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.32.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.32.rkt new file mode 100644 index 0000000..a0420b6 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.32.rkt @@ -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) \ No newline at end of file diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.33.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.33.rkt new file mode 100644 index 0000000..17b7e81 --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.33.rkt @@ -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) \ No newline at end of file diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.7.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.7.rkt new file mode 100755 index 0000000..b2bd89e --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.7.rkt @@ -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) diff --git a/Structure and Interpretation of Computer Programs/sicp-ex-1.8.rkt b/Structure and Interpretation of Computer Programs/sicp-ex-1.8.rkt new file mode 100755 index 0000000..2a8f46d --- /dev/null +++ b/Structure and Interpretation of Computer Programs/sicp-ex-1.8.rkt @@ -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) diff --git a/The C Programming Language/1.1.c b/The C Programming Language/1.1.c new file mode 100644 index 0000000..17da82f --- /dev/null +++ b/The C Programming Language/1.1.c @@ -0,0 +1,8 @@ +#include + +main() +{ + printf("hello,"); + printf(" world"); + printf("\n"); +} diff --git a/The C Programming Language/1.2.c b/The C Programming Language/1.2.c new file mode 100644 index 0000000..7c60220 --- /dev/null +++ b/The C Programming Language/1.2.c @@ -0,0 +1,7 @@ +#include + +main() +{ + // "Unknown escape sequence" + printf("i am breaking this\j\n"); +} diff --git a/The C Programming Language/1.3.c b/The C Programming Language/1.3.c new file mode 100644 index 0000000..26b5591 --- /dev/null +++ b/The C Programming Language/1.3.c @@ -0,0 +1,20 @@ +#include + +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; + } +} diff --git a/The C Programming Language/1.4.c b/The C Programming Language/1.4.c new file mode 100644 index 0000000..231f85d --- /dev/null +++ b/The C Programming Language/1.4.c @@ -0,0 +1,20 @@ +#include + +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; + } +} diff --git a/The C Programming Language/1.5.c b/The C Programming Language/1.5.c new file mode 100644 index 0000000..806e88b --- /dev/null +++ b/The C Programming Language/1.5.c @@ -0,0 +1,9 @@ +#include + +main() +{ + for (int fahr = 300; fahr >= 0; fahr = fahr - 20) + { + printf("%3d %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32)); + } +} diff --git a/The C Programming Language/1.5.o b/The C Programming Language/1.5.o new file mode 100644 index 0000000..cd13457 Binary files /dev/null and b/The C Programming Language/1.5.o differ diff --git a/The C Programming Language/1.6.c b/The C Programming Language/1.6.c new file mode 100644 index 0000000..11a3102 --- /dev/null +++ b/The C Programming Language/1.6.c @@ -0,0 +1,6 @@ +#include + +main() +{ + printf("EOFing is 1: %d\n", getchar() != EOF); +} diff --git a/The C Programming Language/1.7.c b/The C Programming Language/1.7.c new file mode 100644 index 0000000..7dd4728 --- /dev/null +++ b/The C Programming Language/1.7.c @@ -0,0 +1,6 @@ +#include + +main() +{ + printf("EOF: %d\n", EOF); +} diff --git a/The C Programming Language/README.md b/The C Programming Language/README.md new file mode 100644 index 0000000..02432df --- /dev/null +++ b/The C Programming Language/README.md @@ -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). diff --git a/The C Programming Language/a.out b/The C Programming Language/a.out new file mode 100755 index 0000000..028ad2d Binary files /dev/null and b/The C Programming Language/a.out differ