Last active
January 4, 2016 06:08
-
-
Save bryanwoods/8579467 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(/ | |
(+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) | |
(* 3 (- 6 2) (- 2 7))) | |
(defn square [x] | |
(* x x)) | |
(defn sum-of-squares [x y] | |
(+ (square x) (square y))) | |
; How might I accept a variable number of arguments and avoid any conditional logic? | |
(defn f [& args] | |
(let [two-biggest (take-last 2 (sort args))] | |
(let [[x y] two-biggest] | |
(sum-of-squares x y)))) | |
(f 8 2 17 15 82 987) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
square :: Int -> Int | |
square x = x * x | |
sumOfSquares :: [Int] -> Int | |
sumOfSquares [] = 0 | |
sumOfSquares xs = sum $ map square xs | |
f :: Int -> Int -> Int -> Int | |
f x y z = sumOfSquares twoBiggest | |
where twoBiggest = take 2 $ (reverse . sort) [x, y, z] | |
main :: IO() | |
main = do | |
(putStrLn . show) $ f 8 2 17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(/ | |
(+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) | |
(* 3 (- 6 2) (- 2 7))) | |
(define (square x) | |
(* x x)) | |
(define (sum-of-squares x y) | |
(+ (square x) (square y))) | |
(define (f x y z) | |
(if (>= x y) | |
(sum-of-squares x (if (>= y z) y z)) | |
(sum-of-squares y (if (>= x z) x z)))) | |
(f 5 8 9) | |
; Applicative order: call by value, Normal order: call by name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment