Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of '$' sign in String manipulation in Haskell?

Tags:

string

haskell

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  
like image 310
dan9131 Avatar asked May 11 '15 08:05

dan9131


People also ask

What does dollar sign do in Haskell?

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).

How do you represent a String in Haskell?

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.

Is dollar an operator?

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.

How do I convert a String to a list in Haskell?

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"]?

What is string in Haskell?

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.

What is strappend in Haskell?

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.

What is concatenation generalised in Haskell?

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.

What is string manipulation in shell script?

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.


2 Answers

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
like image 161
chi Avatar answered Oct 24 '22 10:10

chi


$ 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 $.

like image 31
Gabriel Ciubotaru Avatar answered Oct 24 '22 12:10

Gabriel Ciubotaru