Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:09:32+00:00 2026-06-13T23:09:32+00:00

In Haskell, I have defined a polymorphic data type Subst a with a single

  • 0

In Haskell, I have defined a polymorphic data type Subst a with a single constructor S :: [(String, a)] -> Subst a as so:

data Subst a where
    S :: [(String, a)] -> Subst a
    deriving (Show)

I want to define a function get::String -> Subst a -> Maybe a that takes a variable name and a substitution, and returns the value for which that variable must be substituted. If the substitution is not defined on the variable, the function should return Nothing.

I’ve tried the following:

get :: String -> Subst a -> Maybe a
get str (S[]) = Nothing
get str (S((a,b):xs)) = if str == a then Just b
    else get str xs

But I’m getting errors. Any ideas why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:09:33+00:00Added an answer on June 13, 2026 at 11:09 pm
    get str (S((a,b):xs)) = if str == a then Just b
        else get str xs
    

    xs is a list of type [(String, a)], but the second argument of get must be a Subst a. It would work if you used a constructor to construct a value of the correct type,

    get str (S((a,b):xs)) = if str == a then Just b
        else get str (S xs)
    

    but it is simpler to use the list directly to look up the variable,

    Prelude> :t lookup
    lookup :: Eq a => a -> [(a, b)] -> Maybe b
    

    so

    get str (S xs) = lookup str xs
    

    does exactly what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a substitution algorithm in Haskell. I have defined a polymorphic
I have developed a cumulative sum function as defined below in the Haskell library
Suppose that in a Haskell program I have some data whose type is something
I've defined a Tree data type in Haskell and an associated 'size' method which
I have a data called Sample, defined as data Sample = Test1 (String,[[String]]) |
In Haskell I can define following data type: data Tree = Empty | Leaf
I have a haskell file test.hs. In this file I have written a function
I have the following function defined: ex 1 x = 1 ex 0 x
Possible Duplicate: Iterating through a String and replacing single chars with substrings in haskell
I have a simple f# quick sort function defined as: let rec qsort(xs:List<int>) =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.