Elm List comprehensions, nth elements of the search list

When I noticed that Elm does not support list comprehension, I tried to simulate Rubik’s Cube in Elm. In Haskell or even Python, I would write something like:

ghci> [2*c | c <- [1,2,3,4]]

[2,4,6,8]

I can’t find my way in Elm. The actual list comprehension I have to write is (in Haskell):

ghci> let x = [0,1 ,3,2]
ghci> let y = [2,3,1,0]
ghci> [y !! fromIntegral c | c <- x]

[ 2,3,0,1]

Where fromIntegral :: (Integral a, Num b)=> a -> b converts an integer to Num.

In Elm, I Try using Arrays:

x = Array.fromList [0,1,3,2]
y = Array.fromList [2,3,1,0]
Array.get (Array.get 2 x) y

I started to encounter difficulties with Maybe type:

Expected Type: Maybe number 
Actual Type: Int

Actually, I had to look up to see what they were. I just did something related to the list instead of hard work.

x = [0,1,3,2]
y = [2,3,1,0]

fn = head (drop nx)< br />map fy

I don’t know if this works or is correct, but it works under the circumstances I tried.

I think my two main problems are:

p>

>Does Yushu support list comprehension? (I want to just use the map)
>How to bypass the type in the Array example?
>Is it valid to call head(drop n x) to get the nth element of the list?

Elm will not and will not support list deduction: https://github.com/elm- lang/Elm/issues/147

The style guide Evan said "likes maps, filters and folds", so... use `map:

map ((y !!).fromIntegral) x

either

map (\i-> y !! fromIntegral i) x< /pre> 

The commenter pointed out that (!!) is invalid Elm (it is valid Haskell). We can define it as:

(!!)an = head(drop na), a Total function.

Maybe

(!!)an = case(head(drop na)) of just x-> X Nothing-> crash "(!!) index error"

When I noticed that Elm does not support list comprehension, I tried to simulate Rubik's Cube in Elm. In Haskell or even Python, I would write something like:

ghci> [2*c | c <- [1,2,3,4]]

[2,4,6, 8]

I can’t find my way in Elm. The actual list comprehension I have to write is (in Haskell):

ghci> let x = [0,1,3,2]
ghci> let y = [2,3,1,0]
ghci> [y !! fromIntegral c | c <- x]
< br />[2,3,0,1]

where fromIntegral :: (Integral a, Num b)=> a –> b converts an integer to Num.

In In Elm, I tried to use Arrays:

x = Array.fromList [0,1,3,2]
y = Array.fromList [2,3, 1,0]
Array.get (Array.get 2 x) y

I started to encounter difficulties with Maybe type:

Expected Type: Maybe number
Actual Type: Int

In fact , I had to look up to see what they were. I just did something related to the list instead of hard work.

x = [0,1,3, 2]
y = [2,3,1,0]

fn = head (drop nx)
map fy

I don’t know if this It works or is correct, but it works under the circumstances I tried.

I think my two main questions are:

>Does elm support list comprehension? (I want to just use the map)
>How to bypass the type in the Array example?
>Is it valid to call head(drop n x) to get the nth element of the list?

Elm will not and will not support list comprehension: https://github.com/elm-lang/Elm/issues/147

p>

The style guide Evan said "like maps, filters and folds", so... use `map:

map ((y !!).fromIntegral ) x

either

map (\i-> y !! fromIntegral i) x

The commenter pointed out (!! ) Invalid Elm (it is valid Haskell). We can define it as:

(!!)an = head(drop na), a total function.

Maybe< /p>

(!!)an = case(head(drop na))of just x-> X Nothing-> crash "(!!) index error"

< /p>

Leave a Comment

Your email address will not be published.