Skip to content

Instantly share code, notes, and snippets.

@BenBals
Last active April 7, 2016 16:32
Show Gist options
  • Save BenBals/19d75d058456fb5099c1ce75cd1f9172 to your computer and use it in GitHub Desktop.
Save BenBals/19d75d058456fb5099c1ce75cd1f9172 to your computer and use it in GitHub Desktop.
Just some beginner practice problems for Elm.

Elm practice problems

Problems

1 Simple functions

  • 1. 1 Write a function that takes an Integer and returns True or False depending on whether the number is greater than 12.

  • 1. 2 Write a function that takes an Integer and returns True or False depending on whether it is odd. Look up the % operator

  • 1. 3 Write a function that takes a name as a String and returns "Hello, Name!". Look up the ++ operator.

    greet "Bob" -> "Hello, Bob!"
    greet "Carl" -> "Hello, Carl!"
    
  • 1. 4 Write a function that takes two numbers and return their sum.

  • 1. 5 Write a function that takes tree numbers and takes the largest one. Hint: use max

  • 1. 6 Write a function that takes two numbers returning their product.

  • 1. 7 Write a function that takes a function and two numbers, applies it to the two numbers and then returns the sum of the two results.

  • 1. 8 Write a function that takes an argument, ignores it and return 12

  • 1. 9 Write a function that takes 3 arguments and return the product of the two biggest ones.

  • 1. 10 Write a function that calculates the area of any given square.

  • 1. 11 Write a function that calculates the area of any given triangle.

  • 1. 12 Write a function that solves an equation of form 0=x^2+px+q.

  • 1. 13 Write a function that get a number's absolute value.

  • 1. 14 Write a function that sees if a number's divisble by another.

  • 1. 15 Write a function that calculates a number's squareroot and uses a Maybe to ensure against failure.

  • 1. 16 Write a function that gets the avarage of any given two numbers.

2 A bit more complicated functions

  • 2. 1 Write a function that gets the factorial of any given number.
  • 2. 2 Write a function that gets the avarage of any list of numbers.
  • 2. 3 Write a function that calculates the digit sum of any given two digit integer.
  • 2. 4 Write a function that calculates the sum of any given list of numbers.
  • 2. 5 Write a function that calculates the nth power of any given number.

Solutions

1 Simple functions

  • 1. 1

    greaterThan12 x = x > 12
  • 1. 2

    isOdd x = (x % 2) /= 0
  • 1. 3

    greet name = "Hello, " ++ name ++ "!"
  • 1. 4

    getSum x y = x + y
  • 1. 5

    largestOfThree x y z = max (max x y) z
  • 1. 6

    getProduct x y = x * y
  • 1. 7

    applyFunctionAndGetSum func n1 n2 = (func n1) + (func n2)
  • 1. 8

    ignoreArg x = 12
  • 1. 9

    productOfGreatest x y z = (x * y * z) / (min x (min y z))

2 A bit more complicated functions

  • 2. 1

    factorial n = if n == 0 then 1 else n * factorial (n-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment