public
Created

  • Download Gist
a_plus_abs_b.hs
Haskell
1 2 3 4 5 6 7 8 9 10
add :: Integer -> Integer -> Integer
add (a, b) = a + b;
 
-- Haskell allows me to call the function two ways
 
-- prefix notation
add (1,2) -- 3
 
-- infix notation
1 `add` 2 -- 3
a_plus_abs_b.js
JavaScript
1 2 3 4 5 6 7 8 9
function a_plus_abs_b(a, b) {
 
return (b > 0 ? plus : subtract)(a, b);
 
// can't return +; #sadpanda
function plus(a, b) { return a + b; };
// can't return -; #sadpanda
function subtract(a, b) { return a - b; };
}
a_plus_abs_b.ss
Scheme
1 2
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
commented.ss
Scheme
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
 
(a-plus-abs-b 4 -5) ; returns 9
 
(+ 4 -5) ; returns -1
(- 4 -5) ; returns 9
 
(=
(a-plus-abs-b 4 -5)
(+ 4 -5)) ; False
 
(=
(a-plus-abs-b 4 -5)
(- 4 -5)) ; True

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.