From 88db4208d827fa91baaadf77e566131a8313e5a4 Mon Sep 17 00:00:00 2001 From: adampridmore <320922+adampridmore@users.noreply.github.com> Date: Wed, 13 Aug 2025 20:05:56 +0100 Subject: [PATCH 1/6] Initial list type --- Adam_Eddy_FSharp/Adam_Eddy_FSharp.fsproj | 12 ++++++ Adam_Eddy_FSharp/Program.fs | 54 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Adam_Eddy_FSharp/Adam_Eddy_FSharp.fsproj create mode 100644 Adam_Eddy_FSharp/Program.fs diff --git a/Adam_Eddy_FSharp/Adam_Eddy_FSharp.fsproj b/Adam_Eddy_FSharp/Adam_Eddy_FSharp.fsproj new file mode 100644 index 0000000..248c750 --- /dev/null +++ b/Adam_Eddy_FSharp/Adam_Eddy_FSharp.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net9.0 + + + + + + + diff --git a/Adam_Eddy_FSharp/Program.fs b/Adam_Eddy_FSharp/Program.fs new file mode 100644 index 0000000..636268a --- /dev/null +++ b/Adam_Eddy_FSharp/Program.fs @@ -0,0 +1,54 @@ +// For more information see https://aka.ms/fsharp-console-apps +printfn "Hello from F#" + +// let x = () +// +let a: Option = Some("HellO)") +let b: Option = None +// + +// Option a = Some("Hello") + + +// Write a function Nil() which returns an empty list. + + // type NIL = unit + + // let myEmptyList : NIL = () + + // printfn "%O" myEmptyList + + +// type C = C * C | unit +// type E<'a> = Choice1 of 'a | Choice2 of 'a * 'a +// type A = int * int +// type B = {FirstName:string; LastName:string} + +// type B = {head: int; tail:B} | NIL +// type B = int * (Value of B) | NIL +// type A = {head: int; tail:B} + + +// type Gift = +// | Book of Book +// | Chocolate of Chocolate +// | Wrapped of Gift * WrappingPaperStyle +// | Boxed of Gift +// | WithACard of Gift * message:string +// + + + +// type A = | Item of head:int * A | Empty +type A<'T> = | Item of head:'T * A<'T> | Empty + + +let x = Empty // An empty list +let y = Item(8, Empty) // Single item in list +let z = Item(1, Item(2, Empty)) // Two item in list + +printfn "x=%O" x + +printfn "y=%O" y +printfn "z=%O" z + From e62e03ecc3c8a7a35cee775a6029270ee8231738 Mon Sep 17 00:00:00 2001 From: adampridmore <320922+adampridmore@users.noreply.github.com> Date: Wed, 13 Aug 2025 20:33:48 +0100 Subject: [PATCH 2/6] Added nil, head and tail methods --- Adam_Eddy_FSharp/Program.fs | 49 +++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/Adam_Eddy_FSharp/Program.fs b/Adam_Eddy_FSharp/Program.fs index 636268a..5067198 100644 --- a/Adam_Eddy_FSharp/Program.fs +++ b/Adam_Eddy_FSharp/Program.fs @@ -3,8 +3,8 @@ printfn "Hello from F#" // let x = () // -let a: Option = Some("HellO)") -let b: Option = None +// let a: Option = Some("HellO)") +// let b: Option = None // // Option a = Some("Hello") @@ -37,18 +37,51 @@ let b: Option = None // | WithACard of Gift * message:string // - - // type A = | Item of head:int * A | Empty -type A<'T> = | Item of head:'T * A<'T> | Empty - +type ListOf<'T> = | Cons of head:'T * ListOf<'T> | Empty let x = Empty // An empty list -let y = Item(8, Empty) // Single item in list -let z = Item(1, Item(2, Empty)) // Two item in list +let y = Cons(1, Empty) // Single item in list +let z = Cons(1, Cons(2, Empty)) // Two item in list printfn "x=%O" x printfn "y=%O" y printfn "z=%O" z +let nil() = Empty +let createTwoItemList a b = Cons(a, Cons(b, Empty)) + +let a = createTwoItemList 1 2 +printfn "a=%O" a + +let isNil<'T>(list: ListOf<'T>): bool= + match list with + | Empty -> true + | _ -> false + +let head<'T>(list: ListOf<'T>): Option<'T> = + match list with + | Cons( head:'T , _) -> Some(head) + | _ -> None + +let tail<'T>(list: ListOf<'T>): ListOf<'T> = + match list with + | Cons( head:'T , tail) -> tail + | Empty -> Empty + +//Cons of head:'T * ListOf<'T> +// let head2<'T>(list: Cons) : 'T = +// match list with +// | Cons( head:'T , _) -> Some(head) +// | _ -> None + + +printfn "isNil for empty list is %O" (isNil(x)) +printfn "isNil for non empty list is %O" (isNil(y)) + +printfn "head for a list with stuff in %O" (head(y)) + + +printfn "tail for a list with stuff in %O" (tail(z)) +printfn "tail for a list which is empty %O" (tail(x)) From c76de2f8871f53754d381c906647e7f474417d71 Mon Sep 17 00:00:00 2001 From: adampridmore <320922+adampridmore@users.noreply.github.com> Date: Wed, 13 Aug 2025 20:42:49 +0100 Subject: [PATCH 3/6] Added sum --- Adam_Eddy_FSharp/Program.fs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Adam_Eddy_FSharp/Program.fs b/Adam_Eddy_FSharp/Program.fs index 5067198..17c9220 100644 --- a/Adam_Eddy_FSharp/Program.fs +++ b/Adam_Eddy_FSharp/Program.fs @@ -76,12 +76,20 @@ let tail<'T>(list: ListOf<'T>): ListOf<'T> = // | Cons( head:'T , _) -> Some(head) // | _ -> None +let sum(list: ListOf): int = + let rec sumList(list: ListOf, total: int): int = + match head(list) with + | Some(x) -> sumList(tail(list), total + x) + | None -> total + + sumList(list, 0) printfn "isNil for empty list is %O" (isNil(x)) printfn "isNil for non empty list is %O" (isNil(y)) printfn "head for a list with stuff in %O" (head(y)) - printfn "tail for a list with stuff in %O" (tail(z)) printfn "tail for a list which is empty %O" (tail(x)) + +printfn "Sum a list = %O" (sum(z)) From 491f13563b61c0115dc0ade02b412fdd7532c4e7 Mon Sep 17 00:00:00 2001 From: adampridmore <320922+adampridmore@users.noreply.github.com> Date: Wed, 13 Aug 2025 20:55:06 +0100 Subject: [PATCH 4/6] non generic fold --- Adam_Eddy_FSharp/Program.fs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Adam_Eddy_FSharp/Program.fs b/Adam_Eddy_FSharp/Program.fs index 17c9220..4878c34 100644 --- a/Adam_Eddy_FSharp/Program.fs +++ b/Adam_Eddy_FSharp/Program.fs @@ -84,6 +84,12 @@ let sum(list: ListOf): int = sumList(list, 0) +let rec fold(list: ListOf)(acc: int)(folder: (int*int)->int ): int = + match head(list) with + | Some(x) -> fold(tail(list))(folder(acc , x))(folder) + | None -> acc + + printfn "isNil for empty list is %O" (isNil(x)) printfn "isNil for non empty list is %O" (isNil(y)) @@ -93,3 +99,5 @@ printfn "tail for a list with stuff in %O" (tail(z)) printfn "tail for a list which is empty %O" (tail(x)) printfn "Sum a list = %O" (sum(z)) + +printfn "Fold a list = %O" (fold(z)(0)(fun (a,b) -> a + b)) From e2181a383241665d9a0bab4a35c0de784c61a9de Mon Sep 17 00:00:00 2001 From: adampridmore <320922+adampridmore@users.noreply.github.com> Date: Wed, 13 Aug 2025 20:59:39 +0100 Subject: [PATCH 5/6] Generic the fold --- Adam_Eddy_FSharp/Program.fs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Adam_Eddy_FSharp/Program.fs b/Adam_Eddy_FSharp/Program.fs index 4878c34..67d9076 100644 --- a/Adam_Eddy_FSharp/Program.fs +++ b/Adam_Eddy_FSharp/Program.fs @@ -84,7 +84,7 @@ let sum(list: ListOf): int = sumList(list, 0) -let rec fold(list: ListOf)(acc: int)(folder: (int*int)->int ): int = +let rec fold<'L, 'A>(list: ListOf<'L>)(acc: 'A)(folder: ('A*'L)->'A ): 'A = match head(list) with | Some(x) -> fold(tail(list))(folder(acc , x))(folder) | None -> acc @@ -101,3 +101,5 @@ printfn "tail for a list which is empty %O" (tail(x)) printfn "Sum a list = %O" (sum(z)) printfn "Fold a list = %O" (fold(z)(0)(fun (a,b) -> a + b)) + +printfn "Fold a list into a string = %O" (fold(z)("")(fun (a,b) -> sprintf "%s%d" a b)) From 09ff5298ee00c0516ec4bb8dbda875b49358a655 Mon Sep 17 00:00:00 2001 From: adampridmore <320922+adampridmore@users.noreply.github.com> Date: Wed, 13 Aug 2025 21:12:11 +0100 Subject: [PATCH 6/6] Tidy up --- Adam_Eddy_FSharp/Program.fs | 49 +------------------------------------ 1 file changed, 1 insertion(+), 48 deletions(-) diff --git a/Adam_Eddy_FSharp/Program.fs b/Adam_Eddy_FSharp/Program.fs index 67d9076..8ddbfeb 100644 --- a/Adam_Eddy_FSharp/Program.fs +++ b/Adam_Eddy_FSharp/Program.fs @@ -1,51 +1,10 @@ -// For more information see https://aka.ms/fsharp-console-apps -printfn "Hello from F#" - -// let x = () -// -// let a: Option = Some("HellO)") -// let b: Option = None -// - -// Option a = Some("Hello") - - -// Write a function Nil() which returns an empty list. - - // type NIL = unit - - // let myEmptyList : NIL = () - - // printfn "%O" myEmptyList - - -// type C = C * C | unit -// type E<'a> = Choice1 of 'a | Choice2 of 'a * 'a -// type A = int * int -// type B = {FirstName:string; LastName:string} - -// type B = {head: int; tail:B} | NIL -// type B = int * (Value of B) | NIL -// type A = {head: int; tail:B} - - -// type Gift = -// | Book of Book -// | Chocolate of Chocolate -// | Wrapped of Gift * WrappingPaperStyle -// | Boxed of Gift -// | WithACard of Gift * message:string -// - -// type A = | Item of head:int * A | Empty -type ListOf<'T> = | Cons of head:'T * ListOf<'T> | Empty +type ListOf<'T> = | Cons of head:'T * ListOf<'T> | Empty let x = Empty // An empty list let y = Cons(1, Empty) // Single item in list let z = Cons(1, Cons(2, Empty)) // Two item in list printfn "x=%O" x - printfn "y=%O" y printfn "z=%O" z @@ -70,12 +29,6 @@ let tail<'T>(list: ListOf<'T>): ListOf<'T> = | Cons( head:'T , tail) -> tail | Empty -> Empty -//Cons of head:'T * ListOf<'T> -// let head2<'T>(list: Cons) : 'T = -// match list with -// | Cons( head:'T , _) -> Some(head) -// | _ -> None - let sum(list: ListOf): int = let rec sumList(list: ListOf, total: int): int = match head(list) with