[ e | v <- f:fs, q ]
[ e | v <- f:fs, q ]
reduces to [ e | q ] [ v := f ] ++ [ e | v <- fs, q ]
[ e | q ] [ v := f ] ++ [ e | v <- fs, q ]
The output of [ e | v <- f:fs, q ]
[ e | v <- f:fs, q ]
should be a single list. What does it mean to put two lists together in the reduction? I mean you can't just put two lists together like this ["a"]["b"]
.
Also, is the symbol :=
the same as =
?
Without knowing where you've seen this it's hard to know for certain what is meant. [e | q][v := f]
[e | q][v := f]
is not valid Haskell code (barring some creative use of language extensions).
What is probably meant is something more like
[e' | q'] ++ [e | v <- fs, q]
where e'
is e
with all instances of v
in replaced with f
, and q'
is q
with all instances of v
replaced with f
So, for example if f
was 5, e
was v*2
and q
was odd v
we'd have
[v*2 | v <- 5:fs, odd v]
Which would reduce to
[5*2|odd 5] ++ [v*2 | v <- fs, odd v]
Since odd 5
reduces to True
we end up with
[5*2] ++ [v*2 | f<- fs, odd v]
The notation you mention is not Haskell code, but a meta-notation for substitution which is frequently used in programming languages theory.
If e
and t
are Haskell expressions, and x
is a Haskell variable, we write e [x := t]
to denote the expression e
where all the free occurrences of x
have been replaced with t
(and avoiding captures). For example
x [x := t] ===> t
x+3 [x := t] ===> t+3
f x + (\x -> x + 32) x [x := t] ===> f t + (\x -> x + 32) t
[ f x y | y <- [1..x] ] [x := t] ===> [ f t y | y <- [1..t] ]
Again, this is not a Haskell operator, but a "mathematical" meta-level operator which takes as input Haskell code (syntax) and produces as output Haskell code (syntax).
It is usually exploited to define beta reduction on lambdas:
(\x -> e) t ---beta---> e [x := t]
Anyway, in the posted expression
[ e | q ] [ v := f ] ++ [ e | v <- fs, q ]
the first [...]
and the last are Haskell list comprehensions, while [v := f]
is the meta-notation for substitution. For instance, here's a fully evaluated example
[ f x y | x <- 1:2:[] , y <- [0..x] ]
===> definition of list comprehension
[ f x y | y <- [0..x] ] [x := 1] ++ [ f x y | x <- 2:[] , y <- [0..x] ]
===> substitution
[ f 1 y | y <- [0..1] ] ++ [ f x y | x <- 2:[] , y <- [0..x] ]
===> definition of list comprehension
[ f 1 y | y <- [0..1] ]
++ [ f x y | y <- [0..x] ] [x := 2]
++ [ f x y | x <- [] , y <- [0..x] ]
===> substitution
[ f 1 y | y <- [0..1] ] ++ [ f 2 y | y <- [0..2] ] ++ [ f x y | x <- [] , y <- [0..x] ]
===> definition of list comprehension
[ f 1 y | y <- [0..1] ] ++ [ f 2 y | y <- [0..2] ] ++ []
===> many other steps here
[ f 1 0, f 1 1 ] ++ [ f 2 0, f 2 1, f 2 2 ] ++ []
===> concatenation
[ f 1 0, f 1 1, f 2 0, f 2 1, f 2 2 ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.