Skip to content

Instantly share code, notes, and snippets.

@adolfont
Created October 22, 2023 21:24
Show Gist options
  • Save adolfont/343a1d46523862797049764f4ca4eb44 to your computer and use it in GitHub Desktop.
Save adolfont/343a1d46523862797049764f4ca4eb44 to your computer and use it in GitHub Desktop.
Exercises 1.3 FP in Lean
-- Exercises
-- Functional Programming in Lean
-- https://lean-lang.org/functional_programming_in_lean/getting-to-know/functions-and-definitions.html
-- Define the function joinStringsWith with type String -> String -> String -> String that creates a new string by placing its first argument between its second and third arguments. joinStringsWith ", " "one" "and another" should evaluate to "one, and another".
-- What is the type of joinStringsWith ": "? Check your answer with Lean.
-- Define a function volume with type Nat → Nat → Nat → Nat that computes the volume of a rectangular prism with the given height, width, and depth.
def maximum (n : Nat) (k : Nat) : Nat :=
if n < k then
k
else n
def joinStringsWith (s1: String) (s2: String) (s3: String) : String :=
String.append s2 (String.append s1 s3)
#eval joinStringsWith ", " "one" "and another"
#check (joinStringsWith ": ")
def volume (height : Nat) (width : Nat) (depth : Nat) : Nat :=
height * width * depth
def identity (α: Nat): Nat :=
α
@adolfont
Copy link
Author

Live Lean link https://live.lean-lang.org/#code=--%20Exercises%0A--%20Functional%20Programming%20in%20Lean%0A--%20https%3A%2F%2Flean-lang.org%2Ffunctional_programming_in_lean%2Fgetting-to-know%2Ffunctions-and-definitions.html%0A%0A--%20Define%20the%20function%20joinStringsWith%20with%20type%20String%20-%3E%20String%20-%3E%20String%20-%3E%20String%20that%20creates%20a%20new%20string%20by%20placing%20its%20first%20argument%20between%20its%20second%20and%20third%20arguments.%20joinStringsWith%20%22%2C%20%22%20%22one%22%20%22and%20another%22%20should%20evaluate%20to%20%22one%2C%20and%20another%22.%0A--%20What%20is%20the%20type%20of%20joinStringsWith%20%22%3A%20%22%3F%20Check%20your%20answer%20with%20Lean.%0A--%20Define%20a%20function%20volume%20with%20type%20Nat%20%E2%86%92%20Nat%20%E2%86%92%20Nat%20%E2%86%92%20Nat%20that%20computes%20the%20volume%20of%20a%20rectangular%20prism%20with%20the%20given%20height%2C%20width%2C%20and%20depth.%0A%0Adef%20maximum%20(n%20%3A%20Nat)%20(k%20%3A%20Nat)%20%3A%20Nat%20%3A%3D%0A%20%20if%20n%20%3C%20k%20then%0A%20%20%20%20k%0A%20%20else%20n%0A%0Adef%20joinStringsWith%20(s1%3A%20String)%20(s2%3A%20String)%20(s3%3A%20String)%20%20%3A%20String%20%3A%3D%0A%20%20%20String.append%20s2%20(String.append%20s1%20s3)%0A%0A%23eval%20joinStringsWith%20%22%2C%20%22%20%22one%22%20%22and%20another%22%0A%0A%23check%20(joinStringsWith%20%22%3A%20%22)%0A%0Adef%20volume%20(height%20%3A%20Nat)%20(width%20%3A%20Nat)%20(depth%20%3A%20Nat)%20%3A%20Nat%20%3A%3D%0A%20%20height%20*%20width%20*%20depth%0A%0Adef%20identity%20(%CE%B1%3A%20Nat)%3A%20Nat%20%3A%3D%0A%20%20%CE%B1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment