Post History
Haskell, 352 bytes f a=let{(d,e)=y$filter(>'-')a;f=drop 1e;g=0!h;h=fst n;i=scanr(:)[]h;(j,k)=elem '.'f%(1!head(filter(\a->a>[]&&elem(a++a)i)i++[[last$'0':h]]));(l,m)=(read('0':d...
Answer
#2: Post edited
- # [Haskell], 352 bytes
- <!-- language-all: lang-hs -->
- f a=let{(d,e)=y$filter(>'-')a;f=drop 1e;g=0!h;h=fst n;i=scanr(:)[]h;(j,k)=elem '.'f%(1!head(filter(\a->a>[]&&elem(a++a)i)i++[[last$'0':h]]));(l,m)=(read('0':d)::Integer,1)#g#(j,k*snd g);n=y f}in(last$l:[-l|elem '-'a],m)
- y=break(<'0')
- a!b=(b>[])%(read b,10^length b-a)
- (a,b)#(c,d)=let{e=a*d+b*c;f=b*d;g=(`div`gcd e f)}in(e>0)%(g e,g f)
- a%b|a=b|0<1=(0,1)
- [Try it online!][TIO-kth1z7yf]
- [Haskell]: https://www.haskell.org/
- [TIO-kth1z7yf]: https://tio.run/##bVHBcpswEL37KxTHDpJBKsJOpoWIS08990joZDECVAvZA7ipp@m3u0LGM85ML6t9Wu17b1cN9Dup9flcIRBaDn9wGUgiTotK6UF2OPWoRyCpRNntD4jLpBbhXZM0ouoHZBIl@i2YDscky5sE/wx2REgtW@Qxr1piftdIKPHE9QI0hTTLHx7GJxh8H4giyvezTEM/LLzQi5s8JyTBOmiJwN3YPN6WJI6/mUHWsgs4ua/vR6VVb0pUk8SIE6r@KoMdiY4zqt8vHqgHuSWanURhqXb42XKRGdwVAhfWB1k6BVQEPPyhpamHBhUUyAxDUJB7vA1K4nYiBaxKv1ht7R6KVWl3gF9L9eu13pZIooqM4jINLV@NZFDbmxksi3cQxXv4zAUOrelzC8oggWo5fN3bUczQp6k4HIfvQ8eORisje1aq/qDhxFo4uG3hvtm/IQjwi7NE0wv2fe@TF7u8IAv7c4QwN0ocWzZlappmlyQns4kTgRW3wyAjWvit2mO7GFUuYzP7mwRmCFmLTnuSG0DpxdG87buyzzp50GoLg8SGTtsC4iEvgGBO03lQ5ATO2TycB2hOXfRD5s5N5JCL9MnF6FIJP/PH8WTugYvURTYRTAyUueY14xtXXTM2tXA@pZyFUxaxL1MWMh7x2zz6AD6WrjULblH4H3RtpDxabx6f2HisN1cj0a2Sc@neuv6p70p3g9k8/wc "Haskell – Try It Online"
- # [Haskell], 352 bytes
- <!-- language-all: lang-hs -->
- f a=let{(d,e)=y$filter(>'-')a;f=drop 1e;g=0!h;h=fst n;i=scanr(:)[]h;(j,k)=elem '.'f%(1!head(filter(\a->a>[]&&elem(a++a)i)i++[[last$'0':h]]));(l,m)=(read('0':d)::Integer,1)#g#(j,k*snd g);n=y f}in(last$l:[-l|elem '-'a],m)
- y=break(<'0')
- a!b=(b>[])%(read b,10^length b-a)
- (a,b)#(c,d)=let{e=a*d+b*c;f=b*d;g=(`div`gcd e f)}in(e>0)%(g e,g f)
- a%b|a=b|0<1=(0,1)
- Explanation: We first parse the sign, integral part, fractional part and the repeating decimals as four separate rational numbers, then combine them using rational addition and multiplication. Addition implicitly cancels the fraction.
- [Try it online!][TIO-kth1z7yf]
- [Haskell]: https://www.haskell.org/
- [TIO-kth1z7yf]: https://tio.run/##bVHBcpswEL37KxTHDpJBKsJOpoWIS08990joZDECVAvZA7ipp@m3u0LGM85ML6t9Wu17b1cN9Dup9flcIRBaDn9wGUgiTotK6UF2OPWoRyCpRNntD4jLpBbhXZM0ouoHZBIl@i2YDscky5sE/wx2REgtW@Qxr1piftdIKPHE9QI0hTTLHx7GJxh8H4giyvezTEM/LLzQi5s8JyTBOmiJwN3YPN6WJI6/mUHWsgs4ua/vR6VVb0pUk8SIE6r@KoMdiY4zqt8vHqgHuSWanURhqXb42XKRGdwVAhfWB1k6BVQEPPyhpamHBhUUyAxDUJB7vA1K4nYiBaxKv1ht7R6KVWl3gF9L9eu13pZIooqM4jINLV@NZFDbmxksi3cQxXv4zAUOrelzC8oggWo5fN3bUczQp6k4HIfvQ8eORisje1aq/qDhxFo4uG3hvtm/IQjwi7NE0wv2fe@TF7u8IAv7c4QwN0ocWzZlappmlyQns4kTgRW3wyAjWvit2mO7GFUuYzP7mwRmCFmLTnuSG0DpxdG87buyzzp50GoLg8SGTtsC4iEvgGBO03lQ5ATO2TycB2hOXfRD5s5N5JCL9MnF6FIJP/PH8WTugYvURTYRTAyUueY14xtXXTM2tXA@pZyFUxaxL1MWMh7x2zz6AD6WrjULblH4H3RtpDxabx6f2HisN1cj0a2Sc@neuv6p70p3g9k8/wc "Haskell – Try It Online"
#1: Initial revision
# [Haskell], 352 bytes <!-- language-all: lang-hs --> f a=let{(d,e)=y$filter(>'-')a;f=drop 1e;g=0!h;h=fst n;i=scanr(:)[]h;(j,k)=elem '.'f%(1!head(filter(\a->a>[]&&elem(a++a)i)i++[[last$'0':h]]));(l,m)=(read('0':d)::Integer,1)#g#(j,k*snd g);n=y f}in(last$l:[-l|elem '-'a],m) y=break(<'0') a!b=(b>[])%(read b,10^length b-a) (a,b)#(c,d)=let{e=a*d+b*c;f=b*d;g=(`div`gcd e f)}in(e>0)%(g e,g f) a%b|a=b|0<1=(0,1) [Try it online!][TIO-kth1z7yf] [Haskell]: https://www.haskell.org/ [TIO-kth1z7yf]: https://tio.run/##bVHBcpswEL37KxTHDpJBKsJOpoWIS08990joZDECVAvZA7ipp@m3u0LGM85ML6t9Wu17b1cN9Dup9flcIRBaDn9wGUgiTotK6UF2OPWoRyCpRNntD4jLpBbhXZM0ouoHZBIl@i2YDscky5sE/wx2REgtW@Qxr1piftdIKPHE9QI0hTTLHx7GJxh8H4giyvezTEM/LLzQi5s8JyTBOmiJwN3YPN6WJI6/mUHWsgs4ua/vR6VVb0pUk8SIE6r@KoMdiY4zqt8vHqgHuSWanURhqXb42XKRGdwVAhfWB1k6BVQEPPyhpamHBhUUyAxDUJB7vA1K4nYiBaxKv1ht7R6KVWl3gF9L9eu13pZIooqM4jINLV@NZFDbmxksi3cQxXv4zAUOrelzC8oggWo5fN3bUczQp6k4HIfvQ8eORisje1aq/qDhxFo4uG3hvtm/IQjwi7NE0wv2fe@TF7u8IAv7c4QwN0ocWzZlappmlyQns4kTgRW3wyAjWvit2mO7GFUuYzP7mwRmCFmLTnuSG0DpxdG87buyzzp50GoLg8SGTtsC4iEvgGBO03lQ5ATO2TycB2hOXfRD5s5N5JCL9MnF6FIJP/PH8WTugYvURTYRTAyUueY14xtXXTM2tXA@pZyFUxaxL1MWMh7x2zz6AD6WrjULblH4H3RtpDxabx6f2HisN1cj0a2Sc@neuv6p70p3g9k8/wc "Haskell – Try It Online"