Post History
Haskell, 101 95 bytes import Data.List r a=or$map(a?)$(map read$tail.inits=<<tails(show a))\\[a] a?b=b/=0&&a`mod`b==0 Try it online! Changelog: Saved 4 bytes by replacing an...
Answer
#2: Post edited
# [Haskell], 101 bytes- ``` haskell
- import Data.List
r a=any id$map(a?)$(map read.(tail.inits=<<).tails.show$a)\\[a]- a?b=b/=0&&a`mod`b==0
- ```
[Try it online!][TIO-lz0anjqk]- [Haskell]: https://www.haskell.org/
[TIO-lz0anjqk]: https://tio.run/##NU3BboMwDL37K3xAFRzK0hgSqJr1sF0mbacd12o1A7XRIFQk0rSvZ6HVnuRnPz/5@cL@u@v7ebbDdZwCPnPg/NX6ABOyYfeLtk0Gvqa8z5I0Djh13OZpYNvn1tngzW6X5Yv0ub@MPwlnh8MHH4H3jWkejFit@DSM7akxRswDW4cG2xEw4mt0oXMBd2s8d@HprvzNip/ePjG9Tjb6OU6x0uUzbrf4HuL2jOtHfHEhyzDB3rrO/8fNBdAGigJkSUBawEZSUSpYSFdw4xpIRGjQSyNQWspaQyWrQsdGqiQNtaKKKB4IRVGWRSV0DFBUliJqSVJXqq5B3kF/ "Haskell – Try It Online"
- # [Haskell], ~~101~~ 95 bytes
- ``` haskell
- import Data.List
- r a=or$map(a?)$(map read$tail.inits=<<tails(show a))\\[a]
- a?b=b/=0&&a`mod`b==0
- ```
- [Try it online!][TIO-lz1e7fo0]
- [Haskell]: https://www.haskell.org/
- [TIO-lz1e7fo0]: https://tio.run/##NU1NT8MwDL37V/hQTe1hJYvbJJ0adoALEpw4MsRcWm0R/ZiaSPz8km7iSX7285OfL@x/ur5fFjdcpzngMwfOX50PMCPbaU4GvqZ8yJI0Djh33CaBXZ@70QVv63oVPvWX6Rc5y47HD/4EPjS2ebBis@HTMLWnxlqxDOxGtNhOgBHf0xi6MWC9xXMXnu7K36z45@0L0@vsop/jHCtd/@J@j@8hbs@4fcSXMWQZJti7sfP/cUsBtIOiAFkSkBawk1SUClbSBm5cAYkIDXptBEpLWWkw0hQ6NlIlaagUGaJ4IBRFWRZG6BigqCxF1JKkNqqqQN5Bfw "Haskell – Try It Online"
- Changelog:
- * Saved 4 bytes by replacing `any id` with `or`.
- * Saved 2 bytes by η-expanding `map read.(tail.inits=<<).tails.show$a` to `map read$tail.inits=<<tails(show a)`
- Explanation:
- * `tails` returns all suffixes of a given list (note that a string is a list). `inits` returns all prefixes of a given list. By binding (aka `=<<`) the two together, we get all of the contiguous substrings. By also binding `tail`, which drops the first item from a list, we remove the empty string from this list (since `inits` always puts the empty string first in its output). This gives us `\a->tail.inits=<<tails a`
- * We need to first convert our Int to a string, and convert our list of strings back to a list of ints at the end. We do this by adding `show` and `map read`: `\a->map read$tail.inits=<<tails(show a)`
- * But we still need to get rid of the input number itself. Since we've already imported Data.List for `inits` and `tails`, we can use the list difference operator `(\\)`, which saves us some bytes over the `remove` function: `(map read$tail.inits=<<tails(show a))\\[a]`
- * Next we need to check if any of the numbers in the list are factors of the input. For this, we create the `(?)` operator, which checks if its second argument is a factor of its first. Firstly, `0` is not a factor of any number, so we check if the second argument is not zero. (Notice that the "not equal" operator in Haskell is `(/=)`.) Then, we can use the `mod` function to get the modulo and check if that is zero. Iff `b` is not `0` and ```a `mod` b``` is `0`, then `a?b` will return `True`: ```a?b=b/=0&&a`mod`b==0```
- * Then `map` a partial application of the `(?)` operator over our list of substring numbers: `\a->map(a?)$(map read$tail.inits=<<tails(show a))\\[a]`
- * Finally, collapse the list into a single boolean output by using the `or` function, which takes a list of booleans and returns `True` only if any member of the list is `True`.
#1: Initial revision
# [Haskell], 101 bytes ``` haskell import Data.List r a=any id$map(a?)$(map read.(tail.inits=<<).tails.show$a)\\[a] a?b=b/=0&&a`mod`b==0 ``` [Try it online!][TIO-lz0anjqk] [Haskell]: https://www.haskell.org/ [TIO-lz0anjqk]: https://tio.run/##NU3BboMwDL37K3xAFRzK0hgSqJr1sF0mbacd12o1A7XRIFQk0rSvZ6HVnuRnPz/5@cL@u@v7ebbDdZwCPnPg/NX6ABOyYfeLtk0Gvqa8z5I0Djh13OZpYNvn1tngzW6X5Yv0ub@MPwlnh8MHH4H3jWkejFit@DSM7akxRswDW4cG2xEw4mt0oXMBd2s8d@HprvzNip/ePjG9Tjb6OU6x0uUzbrf4HuL2jOtHfHEhyzDB3rrO/8fNBdAGigJkSUBawEZSUSpYSFdw4xpIRGjQSyNQWspaQyWrQsdGqiQNtaKKKB4IRVGWRSV0DFBUliJqSVJXqq5B3kF/ "Haskell – Try It Online"