All Pages Hacks and Guides for Android:
My best solution so far is completely dynamically typed:. This problem requires the existence of an unspecified numeric tower. Lisp happens to have one and it happens to be adequate for Paul Graham's examples because this problem was specifically designed to make Lisp look artificially good. The following solution uses the latter approach:. However, numeric towers are virtually useless in the real world.
Unless you are very careful, trying to learn from these kinds of borked challenges will do you more harm than good. You should leave asking yourself why we do not want a numeric tower, do not want to box and do not want run-time type promotion? We know the type of x at every step. Every number is stored unboxed.
We know that this code will never produce a run-time type error I agree with Jon that this is quite artificial example and it is not a good starting point for learning F.
However, you can use static member constraints to get reasonably close without dynamic casts and reflection. If you mark it as inline and add convert both of the parameters using float :.
The function takes any two arguments that can be explicitly converted to float. The only limitation compared to the LISP version I guess is that it always returns float as the most universal numeric type available. You can write something like:.
It's definitely not possible to implement this with proper static types. You say you can in Haskell, but I don't believe you. The problem with trying to do this with static typing is in adding two different numbers of possibly different types while preserving the type of the left-hand side. As Jon Harrop says this is possible with a union type.
Once you've defined the union type and a corresponding addition operation which works as mentioned, the actual accumulator is very simple.
My implementation:. Learn more. Accumulator generator in F Ask Question.
Asked 9 years, 4 months ago. Active 5 years, 7 months ago. Viewed times. Why is that and can I avoid it? Any ideas to improve this dynamically typed version? Is it possible to implement this with proper static types? Maybe with member constraints?
Subscribe to RSS
Stuart Marks Jon Harrop Jon Harrop 45k 12 12 gold badges silver badges bronze badges. Tomas Petricek Tomas Petricek k 17 17 gold badges silver badges bronze badges. Jon: Yes, that's why I wrote "reasonably close without using dynamic casts". Brian Brian k 15 15 gold badges silver badges bronze badges.
Amazing Resonance Experiment!
You are right. I'm not really well-versed in Haskell.
I saw Haskell on the page of accepted solutions paulgraham. Now that I tried it, I found out that the Haskell solutions does not really seem to fulfill the requirements it statically decides to use floats all the way if you use the generated function with a float anywhere. Add i! Yawar Yawar 8, 3 3 gold badges 39 39 silver badges 65 65 bronze badges. Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.
Email Required, but never shown. How to create micro-interactions with react-spring: Part 1.
All Game&Apps Hacks and Guides for Android:
This week, StackOverflowKnows syntactic sugar, overfit or nah, and the…. Featured on Meta. Thank you, Shog9. Thank you, Robert Cartaino.
Accumulator Based 3-Weight Test Pattern Generation
Has Stack Exchange rescinded moderator access to the featured tag on Meta? How do the moderator resignations affect me and the community?