Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 11, 2016
0 parents commit 1056466
Show file tree
Hide file tree
Showing 135 changed files with 8,377 additions and 0 deletions.
Binary file added 3142.pdf
Binary file not shown.
Binary file added 3143.pdf
Binary file not shown.
Binary file added 9781590596203.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions Chapter02/ch_02.ml
@@ -0,0 +1,18 @@
1 + 1;;
2 + 1;;
3 / 4;;
1. +. 1.;;
Printf.printf "%s" "Hello, World";;
Printf.printf "%s %s" "hello";;

let func x = match x with
Foot y -> Meter x;;

let convert x = match x with
Foot y -> Meter (int_of_float ((float_of_int y) *. 0.3048))
| Meter y -> Foot (int_of_float ((float_of_int y) /. 0.3048))
_ -> x;;

let _ = Printf.printf "Hello World";

(* ocamlmktop -o custom_toplevel foo.cmo bar.cmo *)
51 changes: 51 additions & 0 deletions Chapter03/ch_03.ml
@@ -0,0 +1,51 @@
let a = 1;;
let b = ref 1;;
:= 10;;
b;;
b.contents <- 20;;
b;;
let someval = "hello";;
let otherval = "world" in let someval = "Bummer, " in
Printf.printf "%s %s\n" someval otherval;;


type tree = Leaf of tree | Node of string;;

Leaf (Leaf (Node "terminal"));;

type distance = Meter of int | Foot of int | Mile of int;;

Foot 10;;
Meter 20;;
(Meter 20) + (Foot 10);;

type 'a distance = Meter of int | Foot of int
| Mile of int | Distance of 'a distance;;

Distance (Foot 10);;
Distance 10;;

type `a part_with_length = {part_name: string;
part_number: int; part_length: `a distance };;


let crescent_wrench = {part_name="Left Handed Crescent Wrench";
part_number=1;part_length=(Foot 1)};;


let add x = x + 1;;
add 5;;

let add_plus x = if (x > 10) then
x + x
else
x + 1;;

add_plus 15;;
add_plus 10;;






209 changes: 209 additions & 0 deletions Chapter04/ch_04.ml
@@ -0,0 +1,209 @@
let a = 5;;
let b = 10;;
a + b;;

let myfunc () = 1 + 1;;

myfunc ();;

let myfunc = (fun () -> 1 + 1);;
myfunc ();;
let myfunc x y =
let someval = x + y in
Printf.printf "Hello internal value: %i\n" someval;;

myfunc 10 20;;

let errorprone x = try
while !x < 10 do
incr x
done
with _ -> "Failed";;

let errorprone x = try
while !x < 10 do
incr x
done
with _ -> ();;

let mismatching (x:int) (y: float) = x + (int_of_string y);;

let bigger f x y = f x y;;

bigger (>) 10 20;;
bigger (<) 10 20;;
bigger < 10 20;;

let add_one = (+) 1;;

add_one 10;;

(fun x y -> x * y);;

[1;2;3;4;5];;

List.sort compare [4;5;6;2;4;2;0];;

List.nth [0;1;2;3;4] 3;;

List.nth [0;1;2;3;4] 0;;

List.sort (fun x y -> if x = y then
-1
else if x < y then
1
else
0) [1;4;9;3;2;1];;

List.sort compare [1;4;9;3;2;1];;

Scanf.sscanf "hello world" "%s %s" (fun x y -> Printf.printf "%s %s\n" y x);;

(fun x -> 10);;

(fun x -> 10 + x) 30;;

Printf.printf "%s\n";;

let funclist = [Printf.printf "%s\n"];;

(List.nth funclist 0) "hello world";;

List.fold_left (+) 0 [1;2;3;4;5];;

let sum = List.fold_left (+) 0;;

sum [1;2;3;4;5;6];;

let f x y = x + y;;

let m = [f];;

(List.hd m) == f;;

let b = f;;

b == f;;

let c x y = x + y;;

c == f;;

let compose m y = y m;;

compose (fun x -> x 3.14159) (fun m n o -> (m n) o);;

let b = compose (fun x -> x 3.14159) (fun m n o -> (m n) o);;

b 3.123;;

b (fun n m o -> o);;
(b (fun n m o -> o)) "hi" "there";;

let add_one = (+) 1;;

val add_one : int -> int = <fun

# add_one 10;;

- : int = 11



(Meter 3) %* (Mile 1);;

(Foot 12) %- (Foot 3);;



let rec fib n = if (n < 2) then
1
else
(fib (n - 1)) + (fib (n - 2));;

fib 6;;

let explode_string x =
let strlen = String.length x in
let rec es i acc =
if (i < strlen) then
es (i+1) (x.[i] :: acc)
else
List.rev acc
in
es 0 [];;

let collapse_string x =
let buf = Buffer.create (List.length x) in
let rec cs i = match i with
[] -> Buffer.contents buf
| h :: t -> Buffer.add_char buf h;cs t
in
cs x;;

let wrong_recursive lst acc = match lst with
[] -> acc
| h :: t -> wrong_recursive t ((String.length h) :: acc);;

let rec wrong_recursive lst acc = match lst with
[] -> acc
| h :: t -> wrong_recursive t ((String.length h) :: acc);;

let rec scan_input scan_buf acc_buf = try
Scanf.bscanf scan_buf "%c" (fun x ->
Buffer.add_char acc_buf x);
scan_input scan_buf acc_buf
with End_of_file -> Buffer.contents acc_buf;;

let myfunc x = match x with
n,m,z -> (n+m,z+. 4.);;

myfunc (1,2,3.);;
myfunc 1;;


let myfunc (n,m,z) = (n+m,z+. 0.4);;

let myfunc x = match x with
n,m,z -> n+m+z
| n,m,_ -> n+m;;


let myfunc x = match x with
n,m,_ -> n+m;;

let myfunc (n,m,z) = n+m;;

let rec lispy x acc = match x with
[] -> acc
| head :: tail -> lispy tail (acc + head);;

lispy [1;2;3;4;5] 0;;


let b x y = match x with
0 -> (let q = x in match y with
0 -> 1
| _ -> q)
| 1 -> y
| _ -> x * y;;

b 0 3;;
b 0 0;;

(Foot 0) %%+ (Mile 3);;

(Foot 3) %%+(Mile 3);;


let add_some_labeled ~x ~y = x + y;;

add_some_labeled 10 20;;
add_some_labeled ~x:10 30;;
add_some_labeled ~x:10 ~y:10;;

let increment ?(by = 1) v = v + by;;

increment 10;;
increment ~by:30 10;;
increment 30 10;;
94 changes: 94 additions & 0 deletions Chapter04/distance.ml
@@ -0,0 +1,94 @@
type distance = Meter of int | Foot of int | Mile of int;;

let meter_of_int x = Meter x;;

let to_meter x = match x with

Foot n -> Meter ( n / 3)

| Mile n -> Meter (n * 1600)

| Meter n -> Meter n;;


let to_foot x = match x with

Mile n -> Foot (n * 5000)

| Meter n -> Foot (n * 3)

| Foot n -> Foot n;;


let to_mile x = match x with

Meter n -> Mile (n / 1600)

| Foot n -> Mile (n / 5000)

| Mile n -> Mile n;;

meter_of_int 10;;

let math_on_meter x y z = match x,y with

Meter n, Meter m -> Meter (z n m)

| _ -> raise Not_found;;


let math_on_foot x y z = match x,y with

Foot n,Foot m -> Foot (z n m)

| _ -> raise Not_found;;


let math_on_mile x y z = match x,y with

Mile n,Mile m -> Mile (z n m)

| _ -> raise Not_found;;

let ( %+ ) x y = match x with

Meter n -> math_on_meter x (to_meter y) ( + )

| Foot n -> math_on_foot x (to_foot y) ( + )

| Mile n -> math_on_mile x (to_mile y) ( + );;

let ( %- ) x y = match x with

Meter n -> math_on_meter x (to_meter y) ( - )

| Foot n -> math_on_foot x (to_foot y) ( - )

| Mile n -> math_on_mile x (to_mile y) ( - );;

let ( %* ) x y = match x with

Meter n -> math_on_meter x (to_meter y) ( * )

| Foot n -> math_on_foot x (to_foot y) ( * )

| Mile n -> math_on_mile x (to_mile y) ( * );;


let ( %/ ) x y = match x with

Meter n -> math_on_meter x (to_meter y) ( / )

| Foot n -> math_on_foot x (to_foot y) ( / )

| Mile n -> math_on_mile x (to_mile y) ( / );;

let ( %%+ ) x y = match x with

Foot n when n > 0 -> math_on_foot x (to_foot y) ( + )

| Meter n when n > 0 -> math_on_meter x (to_meter y) ( + )

| Mile n when n > 0 -> math_on_mile x (to_mile y) ( + )

| _ -> raise (Invalid_argument "Not a distance type I have defined");;

0 comments on commit 1056466

Please sign in to comment.