This code checks whether the given password is valid according to the constraints in the requirements
import Data.Char
strong :: String -> Bool
strong password = all ($ password) requirements
where requirements = [minLength 15, any isUpper, any isLower, any isDigit]
minLength n str = n <= length str
Dollar sign. Since complex statements like a + b are pretty common and Haskellers don't really like parentheses, the dollar sign is used to avoid parentheses: f $ a + b is equivalent to the Haskell code f (a + b) and translates into f(a + b).
In Haskell a String is just a list of Char s, indeed type String = [Char] . String is just an "alias" for such list. So all functions you define on lists work on strings, given the elements of that list are Char s.
The dollar sign, $ , is a controversial little Haskell operator. Semantically, it doesn't mean much, and its type signature doesn't give you a hint of why it should be used as often as it is. It is best understood not via its type but via its precedence.
These aren't duplicates. One is converting a string to a list, the other is converting a list of strings to a list. In Haskell "12345" IS in fact ['1','2','3','4','5']. Is that what you want or is it ["1","2","3","4","5"]?
Haskell string is a data type which is used to store the value of variable in the form of string, string is represented by the sequence of character in Haskell or in any other programming language as well. String in Haskell provide different functions to manipulate the value of string object, or to perform any operation on it.
It is basically the same interface as provided by the Strings class. However, every input string is a Haskell String here, thus easing the usage of different string types with native Haskell String literals. For example strAppend suffix works with any string type for which an instance of Str is defined.
concat generalised. Glue together multiple strings by a given Haskell String . Appends the given Haskell String to the string. ++ generalised. Cons generalised. Strips white space characters off both ends of the string. Appends the given character n times to the left, such that the resulting string has the given length.
String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands. 1.
all ($ password) requirements
where requirements = [minLength 15, any isUpper, any isLower, any isDigit]
By definition of all
, the above is equivalent to
($ password) (minLength 15) &&
($ password) (any isUpper) &&
($ password) (any isLower) &&
($ password) (any isDigit)
Now, in Haskell a so-called section (+-*/ x)
stands for (\y -> y +-*/ x)
, whatever is the operator +-*/
(which is $
, in your case). So, we obtain
(minLength 15 $ password) &&
(any isUpper $ password) &&
(any isLower $ password) &&
(any isDigit $ password)
Finally, the operator $
is defined like this: f $ x = f x
, i.e. it is the function application operator. We further simplify the code above as:
minLength 15 password &&
any isUpper password &&
any isLower password &&
any isDigit password
$
means "Function Application" .
> :t ($)
($) :: (a -> b) -> a -> b
$
can be used to provide a parameter to a function. In this case you use it to apply "password" to each function the "requirements" have and have type of:
-- because $ is infix, left and right param are the 1st and 2nd respectively
-- partial apply with the right param will still need the left one (a->b)
:t ( $ "a")
( $ "a") :: ([Char] -> b) -> b --where "b" is a bool in this case
seems familiar ?
:t (\f -> f "a")
(\f -> f "a") :: ([Char] -> b) -> b
$ password
and (\f -> f password)
have the same type and same behaviour: take a function and apply it over a
.
When you have to apply a function over a list, you just put the function alone.
But in this case you have to apply functions from a list to "password". If you omit $
it will try to do "password (any isLower)" for example, which don't make any sense.
But you can do it using a lambda like all (\f -> f password) ...
to apply each function to password
or you can do it with $
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With