Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Created January 15, 2012 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegopacheco/1616761 to your computer and use it in GitHub Desktop.
Save diegopacheco/1616761 to your computer and use it in GitHub Desktop.
Functional Society - Calculator Homework

This gist and a Functional Society Gist to talk about Calculator homework, so people can share links and help each other to get the homework right and make questions, get you question answered and so on and on...

Calculator Homework

We need code a simple calculator in Scala and Haskell:

Calculator, basic operations: +, -, /, * (Remember State and Side Effects are not desired)

@cyberjso
Copy link

cyberjso commented Feb 7, 2012

I'm almost sure that we can do that in haskell but I just don't know how. I would like to create a function that receives a mathematical operator like parameter and execute inside. For other functions we can do something like 'map' and send. Then it'll be executed.
Thanks!

@diegopacheco
Copy link
Author

In Clojure + is a function. In haskell is a operator, If you want use him as function must be this way: (+) 2 2

It`s possible pass Functions as Arguments, but as Far as I know in haskell you need use nomads for that => http://stackoverflow.com/questions/3542397/passing-functions-as-argument

@diegopacheco
Copy link
Author

It`s also possible with Higher Order Functions...

Search for: "Higher Order Functions in Order" => http://learnyouahaskell.com/higher-order-functions

applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)

ghci> applyTwice (++ " HAHA") "HEY"
"HEY HAHA HAHA"

Back to the + here is another sample using Higher Order Functions:

ghci> zipWith' (+) [4,2,5,6] [2,6,2,3]
[6,8,7,9]

Cheers,
Diego Pacheco

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